簡體   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;
}

嗨,大家好。 我有一個問題,我正在編寫代碼,主要是 function, ClosedShape* const r = new Rectangle(p1, p2, p3; p4), ClosedShape* const t = new Triangle(p1, p2; p3)新三角不起作用。 並且錯誤消息說不允許抽象 class 類型“矩形”的 object? 請問你能幫幫我嗎?

使用 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;

正如評論所說,有未實現的虛擬方法clone()print()getLength()需要在Rectangle內部實現。

也許是這樣的:

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

然后,您需要為Triangle class 實現類似的功能來構建東西。 看起來這里也有一些其他的錯誤:

    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