12

私は C++ で学校向けのプロジェクトを持っていますが、1 つの部分で行き詰まっています。幾何学図形を操作するには、演算子 + と * をオーバーロードする必要があります。それは問題ではありませんでしたが、ここでは機能しません。他のすべてのクラスが派生する抽象クラスで、演算子を純粋仮想メソッドとして宣言する必要があります。

#include<iostream>
using namespace std;

class Figabs {
protected:
    int fel;
public:
    int getFEL() { return fel; }
    virtual Figabs operator +()=0; /*this is where I get an error: function returning  abstract class “Figabs” is not allowed : function Figabs::operator+ is a pure virtual function */
};

class Coord {
public: 
    int cx, cy; 
public: 
    Coord (){ 
        cx = cy = 0;
    }

    Coord (const int x, const int y) {
        cx = x;
        cy = y;
    }

    Coord (const Coord &din) { 
        cx = din.cx;
        cy = din.cy;
    }

    ~Coord () { }
    void setX(const int val) { cx = val; } ;
    void setY(const int val) { cy = val; };
    int getX() { return cx; }
    int getY() { return cy; }
};

class Point : public Coord, public Figabs { //one of the figures

public:
    Point() { 
        setX(0);
        setY(0);
        fel = 0;
    }

    Point(const int x, const int y): Coord (x,y) { 
        fel = 0;
    } 

    Point(const Point &din): Coord (din) { 
        fel = din.fel; 
    } 

    ~Point() { } 

    Point operator +(const Coord &vector) { /*this works perfectly when I delete the declaration from the abstract class Figabs, but I don’t know how to make them work together */
        int xp = cx + vector.cx;
        int yp = cy + vector.cy;
        return (Point (xp, yp));
    }

    Point operator *(const Coord &vector) {
        Point temp;
        temp.cx = cx * vector.cx;
        temp.cy = cy * vector.cy;
        return (temp);
    } 
};

ありがとうございます。しばらくお待ちください。これが C++ との初めての接触です。

4

3 に答える 3

3

Figabs純粋な仮想メンバー関数が含まれているため、virtual Figabs operator +()=0;インスタンス化できませんFigabs

検討:

virtual Figabs& operator +()=0; 
/*Now you will not be returning an actual instance but can return derived class instances*
于 2013-05-22T10:49:28.340 に答える
0

質問に関連する有用な情報ビットについては、次のリンクをご覧ください。

仮想関数の戻り値の型のオーバーライドは異なり、共変ではありません

virtual Figabs operator +() = 0;//Here ur not passing any i/p parameters

しかし、派生クラスではパラメーターを渡します

Point operator +(const Coord &vector)//here ur sending i/p parameter .
于 2013-05-22T10:49:39.623 に答える