首页 > 解决方案 > 如何使用参数为向量的方法?

问题描述

#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;
}

如您所见,我使用了一个抽象类Shape,以及三个具体类Circle、Rectangular、Triangle。
我想总结各种形状的区域。如
在此处输入图像描述

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

我想覆盖参数为矢量的函数“区域”。但我的错误是在此处输入图像描述

如何从 '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;
}

我改变并修复!

标签: c++virtual

解决方案


多态性不适用于具体类!

通过声明vector<Shape> collection;,您声明了 a vectorof 、Shapenot of或。您可能希望成为能够利用多态性的类型。CircleTriangleRectangularcollectionvector<Shape*>

您的代码的另一个问题是您没有通过collectiontype vector<Shape>,但collection[i]它只是 type Shape

这可能也可以解释您的错误,因为您的编译器很可能想要解析Shape为. 这是不可能的,因此可能会导致您的编译器错误。vector<Shape*>area

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


推荐阅读