简体   繁体   English

如何分配 class memory?

[英]How can I allocate class memory?

#include <iostream>
#include <vector>
using namespace std;

enum Color { RED, BLUE, YELLOW };

class Shape {
    Color lineColor;
public:
    Color getLineColor() const { return lineColor; }
    virtual Shape* clone() const = 0;
    virtual void print() const = 0;
    virtual float getLength() const = 0;
};

class Point {
    int x;
    int y;
public : 
    Point(const int _x, const int _y) { x = _x; y = _y; }
};

class ClosedShape : public Shape {
public:
    virtual Shape* clone() const = 0;
    virtual void print() const = 0;
    virtual float getLength() const = 0;
};

class Polygon : public ClosedShape {

};

class Triangle : public Polygon {
    Point p1;
    Point p2;
    Point p3;
public:
    Triangle(const Point& _p1, const Point& _p2, const Point& _p3)
        :p1(_p1), p2(_p2), p3(_p3) {}
};

class Rectangle : public Polygon {
    Point p1;
    Point p2;
    Point p3;
    Point p4;
public:
    virtual Shape* clone() const = 0;
    virtual void print() const = 0;
    virtual float getLength() const = 0;
    Rectangle(const Point& _p1, const Point& _p2, const Point& _p3, const Point& _p4)
        :p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
};

class ClosedShapeList {
    vector<Polygon*> v;
public:
    void addShape(Rectangle& const r) {
        v.push_back(&r);
    }
    void addShape(Triangle& const t) {
        v.push_back(&t);
    }
};

int main() {
    Point p1(0, 0), p2(0, 10), p3(20, 20), p4(20, 30);

    ClosedShape* const r = new Rectangle(p1, p2, p3, p4);
    ClosedShape* const t = new Triangle(p1, p2, p3);

    ClosedShapeList list{};
    list.addShape(r);
    list.addShape(t);
    delete r;
    delete t;

    list.print();
    cout << list.getTotalArea() << endl;
}

Hi guys.嗨,大家好。 I have a question, I'm making code and in main function, ClosedShape* const r = new Rectangle(p1, p2, p3; p4), ClosedShape* const t = new Triangle(p1, p2; p3), new Rectangle and new Triangle does not work.我有一个问题,我正在编写代码,主要是 function, ClosedShape* const r = new Rectangle(p1, p2, p3; p4), ClosedShape* const t = new Triangle(p1, p2; p3)新三角不起作用。 and the error message said object of abstract class type "Rectangle" is not allowed?并且错误消息说不允许抽象 class 类型“矩形”的 object? Could you help me please?请问你能帮帮我吗?

I'm seeing these errors related to Rectangle when building with clang:使用 clang 构建时,我看到这些与Rectangle相关的错误:

<source>:70:32: error: allocating an object of abstract class type 'Rectangle'
    ClosedShape* const r = new Rectangle(p1, p2, p3, p4);
                               ^
<source>:49:20: note: unimplemented pure virtual method 'clone' in 'Rectangle'
    virtual Shape* clone() const = 0;
                   ^
<source>:50:18: note: unimplemented pure virtual method 'print' in 'Rectangle'
    virtual void print() const = 0;
                 ^
<source>:51:19: note: unimplemented pure virtual method 'getLength' in 'Rectangle'
    virtual float getLength() const = 0;

as the comments have said there are unimplemented virtual methods clone() , print() and getLength() that need to be implemented inside of Rectangle .正如评论所说,有未实现的虚拟方法clone()print()getLength()需要在Rectangle内部实现。

Maybe something like this:也许是这样的:

class Rectangle : public Polygon {
...
public:
    Shape* clone() const override {
        return new Rectangle(p1, p2, p3, p4);
    }
    void print() const override {
        std::cout << "Rectangle"; // TODO: Actually print stuff
    }
    float getLength() const override {
        return 0.0; // TODO: Actually compute the length
    }
...
};

Then you'll need to implement similar functions for the Triangle class to get things building.然后,您需要为Triangle class 实现类似的功能来构建东西。 It looks like there are a few other bugs here too though:看起来这里也有一些其他的错误:

    ClosedShape* const r = new Rectangle(p1, p2, p3, p4);
    ClosedShape* const t = new Triangle(p1, p2, p3);

    ClosedShapeList list{};
    // 1: addShape takes a Rectangle or Triangle reference but r and t are both ClosedShapes pointers, maybe the interface should accept a Polygon pointer or a ClosedShape pointer? You can convert child classes to parent classes, but can't (in general) convert parent classes to child class.
    list.addShape(r);
    list.addShape(t);

    // 2: Since addShape adds the pointer to the object to the vector, deleting these leaves those pointers dangling. You'll want to move these functions to the end (or maybe in the destructor of the `ClosedShapeList` class).
    delete r;
    delete t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM