如何使用参数为向量的方法?

How can I use method of which parameter is vector?

#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

class Shape{
protected:
    int _r;
    int _w;
    int _h;

public:
    Shape(double r) : _r(r) {}
    Shape(double w, double h) : _w(w), _h(h) {}

    virtual double area(vector<Shape *>){
        cout << "shape:: area " << endl;
        return _r;
    }
};

class Circle : public Shape{

public:
    Circle(double r) : Shape(r) {}
    double area(vector<Shape *>) { return _r*_r*atan(1)*4.0; }
};

class Triangle : public Shape{

public:
    Triangle(double s) : Shape(s) {}
    double area(vector<Shape *>) { return sqrt(3) * pow(_r, 2) / 4; }
};

class Rectangular : public Shape{

public:
    Rectangular(double w, double h) :Shape(w, h) {}
    double area(vector<Shape *>) { return  _w * _h ;}
};

int main()
{

    int n;
    char info;
    int value;
    int value2;
    double sum;
    vector<Shape > collection;
    vector<int> answer;

    sum = 0;

    cin >> n;

    for(int i = 0 ; i < n; i++)
    {
        cin >> info;
        if (info == 'C')
        {
            cin >> value;
            Circle c(value);
            collection.push_back(c);
        }
        else if (info == 'R')
        {
            cin >> value;
            cin >> value2;
            Rectangular r(value, value2);
            collection.push_back(r);
        }
        else
        {
            cin >> value;
            Triangle t(value);
            collection.push_back(t);
        }
    }
    for (int i = 0; i < n ; i++)
    {
        sum += collection[i].area(&collection[i]);
    }
    cout << sum << endl;
}

如您所见,我使用了一个抽象的 class 形状,以及三个具体的 class 圆形、矩形、三角形。
我想对所有形状的区域求和。比如

第一个输入表示我们要计算多少个形状。 C 代表圆形,R 代表矩形,T 代表正三角形。

而且我想覆盖参数为向量的函数“area”。 但是我的错误是

如何解决这个从 'std::__1::__vector_base<Shape, std::__1::allocator >::value_type'

的不可行转换
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

class Shape{
protected:
    int _r;
    int _w;
    int _h;

public:
    Shape(double r) : _r(r) {}
    Shape(double w, double h) : _w(w), _h(h) {}
    virtual double area(vector<Shape *>) = 0;
};

class Circle : public Shape{
public:
    Circle(double r) : Shape(r) {}
    double area(vector<Shape *>) { return _r*_r*atan(1)*4.0; }
};

class Triangle : public Shape{
public:
    Triangle(double s) : Shape(s) {}
    double area(vector<Shape *>) { return sqrt(3) * pow(_r, 2) / 4; }
};

class Rectangle : public Shape{
public:
    Rectangle(double w, double h) :Shape(w, h) {}
    double area(vector<Shape *>) { return  _w * _h ;}
};

int main()
{

    int n;
    char info;
    int value;
    int value2;
    double sum;
    vector<Shape*> collection;
    vector<int> answer;

    sum = 0;

    cin >> n;

    for(int i = 0 ; i < n; i++)
    {
        cin >> info;
        if (info == 'C')
        {
            cin >> value;
            Circle c(value);
            collection.push_back(&c);
        }
        else if (info == 'R')
        {
            cin >> value;
            cin >> value2;
            Rectangle r(value, value2);
            collection.push_back(&r);
        }
        else
        {
            cin >> value;
            Triangle t(value);
            collection.push_back(&t);
        }
    }
    for (int i = 0; i < n ; i++)
    {
        sum += collection[i]->area(collection);
    }
    cout << fixed << setprecision(2) << sum << endl;
}

我更改并修复了!

多态性不适用于具体 类!

通过声明 vector<Shape> collection;,您声明 Shapevector,而不是 CircleTriangleRectangular。您可能希望 collection 属于 vector<Shape*> 类型以便能够利用多态性。

您的代码的另一个问题是您没有传递类型为 vector<Shape>collection,而是类型为 Shapecollection[i]

这可能也可以解释您的错误,因为您的编译器很可能希望将 Shape 解析为 vector<Shape*>,因为这是 area 的参数类型。这是不可能的,因此可能会导致您的编译器错误。

此外,如果您只想传递 collection,则必须确保类型匹配。 vector<Shape> 不能隐式转换为 vector<Shape*>