我已經看了這個代碼幾個小時了,但我找不到為什么我不能實體化類。所以我有介面:
class ICloneable {
public:
virtual ICloneable* clone() const = 0;
virtual ~ICloneable() = 0 {}
};
class IPrintable
{
protected:
virtual void print(std::ostream&) const = 0;
public:
virtual ~IPrintable() = 0;
friend std::ostream& operator<<(std::ostream, const IPrintable&);
};
std::ostream& operator<<(std::ostream os, const IPrintable& other) {
other.print(os);
return os;
}
class IComparable {
protected:
virtual bool is_greater(const IComparable& other) const = 0;
virtual bool is_equal(const IComparable& other) const = 0;
public:
virtual ~IComparable() = 0;
virtual bool operator>(const IComparable& other) const {
return is_greater(other);
}
virtual bool operator<(const IComparable& other) const {
return !(is_greater(other) || is_equal(other));
}
virtual bool operator==(const IComparable& other) const {
return is_equal(other);
}
virtual bool operator!=(const IComparable& other) const {
return !(is_equal(other));
}
};
我有兩個繼承這些介面的類:
class I2DShape : public IComparable, public IPrintable {
public:
virtual void print(std::ostream& os) const override final {
os << "Circumference: " << this->circumference();
}
virtual bool is_greater(const I2DShape& other) const final {
return this->circumference() > other.circumference();
}
virtual bool is_equal(const I2DShape& other) const final {
return this->circumference() == other.circumference();
}
virtual double circumference() const = 0;
virtual ~I2DShape();
};
class IPositionable : public IPrintable, public IComparable {
public:
virtual void print(std::ostream& os) const override final {
}
virtual bool is_greater(const IPositionable& other) const final {
distance_from_origin() > other.distance_from_origin();
}
virtual bool is_equal(const IPositionable& other) const final {
distance_from_origin() == other.distance_from_origin();
}
virtual double distance_from_origin() const {
return sqrt(pow(center().get_x(), 2) pow(center().get_y(), 2));
}
virtual Point center() const = 0;
virtual ~IPositionable();
};
最后這兩個類被一個代表形狀的類繼承:
class Shape2D : public IPositionable, public I2DShape, public ICloneable {
protected:
int num_of_points;
Point* points;
public:
Shape2D() : num_of_points(0), points(nullptr) {}
Shape2D(int num) : num_of_points(num), points(new Point[num]) {}
Shape2D(const Shape2D& other) : num_of_points(other.num_of_points) {
points = new Point[num_of_points];
for (int i = 0; i < num_of_points; i ) {
points[i] = other.points[i];
}
}
Shape2D& operator=(const I2DShape& other) {
}
virtual Shape2D* clone() const override = 0;
virtual ~Shape2D() {
if(points)
delete[] points;
}
};
當我從 Shape2D 派生 Square 并創建用于克隆的函式時,我得到它是抽象類的錯誤:
class Square : public Shape2D {
private:
double side;
public:
Square() {}
Square(double s, Point center) : side(s), Shape2D(1) { points[0] = center;}
virtual Point center() const override{
return points[0];
}
virtual double circumference() const override {
return 4 * side;
}
virtual Square* clone() const override final {
return new Square(*this); //error on this line
}
};
錯誤:不允許抽象型別別“Square”的物件
uj5u.com熱心網友回復:
因為在介面中你宣告了解構式= 0是強制在可以實體化的子類中顯式實作它。
有兩種方法可以修復它。
= 0使用{}或使用默認解構式(no )以標準方式創建介面類= default。- 添加顯式解構式
Square
請注意,帶有實作的純虛擬解構式被 gcc 和 clang 視為錯誤。
相關的問題:
- 為什么我們需要 C 中的純虛解構式?
- 帶實作的純虛函式
uj5u.com熱心網友回復:
該錯誤如@HolyBlackCat 在評論中所述,具有功能,is_greater并且is_equal因為它們在被覆寫時具有不同的引數。解決這個問題的簡單方法是= 0從這兩個函式中洗掉,IComparable這樣它們就不是純函式了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/386958.html
上一篇:通過組合動態實作方法
