我有一个C++学校项目,我被困在一个部分: 我必须重载运算符+和*来处理几何图形。这是没有问题的,但是在这里它不起作用:我必须将操作符声明为纯虚方法,在所有其他类从其派生的抽象类中声明。纯虚拟运营商
#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++。
您是否尝试将该声明放入.H文件中? – Shark
@Shark:那会发生什么变化? – Mat
虚拟Figabs运算符+()= 0不带参数 - >返回不同类型的点运算符+(const Coord&vector)?他们的签名必须是一样的... – Exceptyon