gpt4 book ai didi

c++ - 多态性并在C++中定义不同对象的列表

转载 作者:行者123 更新时间:2023-12-02 09:56:08 26 4
gpt4 key购买 nike

为什么我的代码返回错误?我定义了一个名为Shape的父类和两个派生类。我正在尝试存储由类定义的对象,并将它们存储在列表中。

# include <iostream>

using namespace std;

class Shape{
public:
virtual double area(const double &height, const double &weight) const = 0;
};

class Triangle: public Shape{
public:
double height, weight;
Triangle(double height, double weight): height(height), weight(weight){}
double area(){
return (height*weight)/2;
}
};

class Rectangle: public Shape{
public:
double height, weight;
Rectangle(double height, double weight): height(height), weight(weight){}
double area(){
return height*weight;
}
};

Shape *shapes[3];
shapes[0] = new Triangle(2, 1);
shapes[1] = new Rectangle(3, 2);
shapes[2] = new Rectangle(5, 2);

double * show(Shape *shapes){
double arr[3];
for (int i=0; i < 3; i++){
arr[i] = shapes[i].area();
}
return arr;
}

int main(){
double arr[3] = show(shapes);
cout << arr[0] << endl;
cout << arr[1] << endl;
cout << arr[2] << endl;
}

但是我收到以下两个错误:

error: 'shapes' does not name a type Shapes[0] = new Trangle(2,1);

error: cannot convert 'shape**' to 'shape*' double arr[3] = show(shapes);

最佳答案

您的代码有几个问题:

  • 您的抽象基类Shape具有接受参数的纯虚函数area(),但您的派生类则不接受。因此,它也使派生类成为abstract,因此您无法初始化派生类对象。
  • 您不能声明指针Shapes* shapes[3];的数组,然后在全局范围中单独分配它们。这是您遇到的第一个错误。更完整的说明,请参见here
  • 调用show(shapes)函数时,shapesShapes*数组的名称,它衰减为指向第一个元素的指针。结果类型应为Shapes**,这是您遇到的第二个错误。
  • shapes[i]函数中的show()是指向对象的指针,因此您应该使用->而不是.
  • 您的double arr[3];是堆栈上的一个数组,因此函数完成并返回后,其内容将被删除。未将指针返回到已存储的元素是未定义的。
  • 您没有对delete创建的所有对象进行new,导致内存泄漏。

  • 为了使代码正常运行,对代码所做的最小更改是:

    # include <iostream>

    using namespace std;

    class Shape {
    public:
    virtual double area() const = 0;
    };

    class Triangle : public Shape {
    public:
    double height, weight;
    Triangle(double height, double weight) : height(height), weight(weight) {}
    double area() const override { //add override to let compiler check for correctness
    return (height * weight) / 2;
    }
    };

    class Rectangle : public Shape {
    public:
    double height, weight;
    Rectangle(double height, double weight) : height(height), weight(weight) {}
    double area()const override {
    return height * weight;
    }
    };

    Shape* shapes[3] = { new Triangle(2, 1), new Rectangle(3, 2) ,new Rectangle(5, 2) };

    double* show(Shape** shapes) {
    double* arr = new double[3];
    for (int i = 0; i < 3; i++) {
    arr[i] = shapes[i]->area();
    delete shapes[i]; //free the object pointed in shapes[]
    }
    return arr;
    }

    int main() {
    double* arr = show(shapes);
    cout << arr[0] << endl;
    cout << arr[1] << endl;
    cout << arr[2] << endl;
    delete[] arr;
    }

    但是有一些建议可以改进,例如在不使用 new而不是堆的情况下在堆栈上创建对象,以及使用 std::array创建数组等。

    关于c++ - 多态性并在C++中定义不同对象的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60014544/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com