我在Eclipse CDT中得到错误“Class'Polygon'具有虚拟方法'area'但非虚拟析构函数”。为什么?代码段:
标题文件:
class Shape {
public:
virtual ~Shape();
protected:
virtual double area() const = 0;
}
class Polygon : public Shape {
public:
~Polygon();
protected:
double area() const;
private:
Vertex* vertices;
}
实现:
Polygon::~Polygon() {delete[] this->vertices;}
double Polygon::area() const {
...
return areaSum;
}
答案 0 :(得分:7)
听起来像是日食中的一个错误,或者它可能是一个关于小问题的'风格'警告。 Polygon确实有一个虚拟析构函数,因为它的基类析构函数是虚拟的。
答案 1 :(得分:-2)
试试这个:
class Shape {
public:
virtual ~Shape() {}
protected:
virtual double area() const = 0;
}
class Polygon : public Shape {
public:
virtual ~Polygon();
protected:
double area() const;
private:
Vertex* vertices;
}
对于我所面临的问题,这对我有用!