“标记为覆盖但不覆盖” OOP 代码中的问题

发布于 2025-01-16 00:49:44 字数 1875 浏览 3 评论 0原文

我正在尝试用 C++ 练习 OOP,但遇到了有关函数重写的问题。在我的 Shape2D 和 Shape3D 类中,我在 Square 和 Sphere 类(分别为 ShowArea() 和 ShowVolume())中重新定义了虚拟函数。但是,当我重新定义该函数并尝试运行 main 时,它会输出错误:

Shapes.cpp:88:14: error: 'void Square::ShowArea() const' marked 'override', but does not override
         
void ShowArea() const override{

Shapes.cpp:353:14: error: 'void Sphere::ShowVolume() const' marked 'override', but does not override
         
void ShowVolume() const override {

下面是来自 Shape2D、Square、Shape3D 和 Sphere 类的相关代码片段。

class Shape2D : virtual public Shape {

public:

    virtual float Area() const = 0;

    void ShowArea() const;

    virtual string GetName2D() const = 0;
}



class Square: public Shape2D {

private:
    float squareLen;

public:

    // Constructors
    Square() {
        squareLen = 0;
    }

    Square(float len) {
        squareLen = len;
    }

    string GetName2D() const override {
        string res;

        return res;
    }

    // Returns the area of the shape
    float Area() const override {
        return (squareLen * squareLen);
    }

    void ShowArea() const override{
        cout << "Square Area: " << endl;
    }
}


class Shape3D : virtual public Shape {
    public:
        virtual float Volume() const = 0;
        void ShowVolume() const;
        virtual string GetName3D() const = 0;
}



class Sphere: public Shape3D {

private:
    Circle* SphereBase;

public:
    Sphere() {
        SphereBase = new Circle();
    }

    Sphere(float radius) {
        SphereBase = new Circle(radius);
    }

    float Volume() const {
        return (1.3333 * pi * pow(SphereBase->GetRadius(), 3));
    }

    void ShowVolume() const override {

    }

为什么当我在子类中重新定义函数时会出现这种情况,而该函数在其原始定义中是虚拟的?它不适用于我的任何形状(我有 6 个形状,但本文中只包含 2 个),所以我不认为这是一个拼写错误,而且它在 2D 和 3D 形状上都会崩溃,所以对于那些特定的类来说这不是问题。

I am trying to practice OOP in C++ but I am running into an issue regarding overriding of functions. In my Shape2D and Shape3D classes, I have virtual functions which I redefine in the Square and Sphere classes (ShowArea() and ShowVolume() respectively). However, when I redefine the function and try to run the main, it outputs the errors:

Shapes.cpp:88:14: error: 'void Square::ShowArea() const' marked 'override', but does not override
         
void ShowArea() const override{

Shapes.cpp:353:14: error: 'void Sphere::ShowVolume() const' marked 'override', but does not override
         
void ShowVolume() const override {

Below is a snippet of relevant code from both the Shape2D, Square, Shape3D, and Sphere classes.

class Shape2D : virtual public Shape {

public:

    virtual float Area() const = 0;

    void ShowArea() const;

    virtual string GetName2D() const = 0;
}



class Square: public Shape2D {

private:
    float squareLen;

public:

    // Constructors
    Square() {
        squareLen = 0;
    }

    Square(float len) {
        squareLen = len;
    }

    string GetName2D() const override {
        string res;

        return res;
    }

    // Returns the area of the shape
    float Area() const override {
        return (squareLen * squareLen);
    }

    void ShowArea() const override{
        cout << "Square Area: " << endl;
    }
}


class Shape3D : virtual public Shape {
    public:
        virtual float Volume() const = 0;
        void ShowVolume() const;
        virtual string GetName3D() const = 0;
}



class Sphere: public Shape3D {

private:
    Circle* SphereBase;

public:
    Sphere() {
        SphereBase = new Circle();
    }

    Sphere(float radius) {
        SphereBase = new Circle(radius);
    }

    float Volume() const {
        return (1.3333 * pi * pow(SphereBase->GetRadius(), 3));
    }

    void ShowVolume() const override {

    }

Why is this the case when I am redefining the function in the subclasses and the function is virtual in its original definition? It does not work for any of my shapes (I have 6 shapes but only included 2 in this post) so I dont think its a typo and its crashes across both 2D and 3D shapes so its not an issue with those specific classes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅冕 2025-01-23 00:49:44

Shape2D 类中声明的函数 ShowArea

void ShowArea() const;

不是虚函数。所以派生类Square中的这个声明

void ShowArea() const override{
    cout << "Square Area: " << endl;
}

是不正确的。

此外,类 Shape3D 中声明的函数 ShowVolume 不是虚函数

void ShowVolume() const;

,它不能在派生类中被重写。

您需要在基类中将函数声明为虚拟函数,以便可以重写它们。

The function ShowArea declared in the class Shape2D

void ShowArea() const;

is not a virtual function. So this declaration in the derived class Square

void ShowArea() const override{
    cout << "Square Area: " << endl;
}

is incorrect.

Also the function ShowVolume declared in the class Shape3D is not a virtual function

void ShowVolume() const;

It may not be overridden in a derived class.

You need to declare the functions to be virtual in base classes that they could be overridden.

画骨成沙 2025-01-23 00:49:44

问题是目前成员函数showAreashowVolume不是虚拟成员函数,我们可以使用override 关键字仅在重写虚拟成员函数时。

解决这个问题,您需要通过添加关键字virtual来创建showAreashowVolume虚拟成员函数,如下所示:

class Shape2D : virtual public Shape {

public:

//--vvvvvvv------------------------->virtual added here
    virtual void ShowArea() const;
    //other code here 
};
class Shape3D : virtual public Shape {
    public:
//------vvvvvvv------------------------------>virtual  added here        
        virtual void ShowVolume() const;
        //other code here
};
//other code here

The problem is that currently the member functions showArea and showVolume are not virtual member functions and we can use override keyword only when overriding a virtual member function.

To solve this you need to make showArea and showVolume virtual member functions by adding the keyword virtual as shown below:

class Shape2D : virtual public Shape {

public:

//--vvvvvvv------------------------->virtual added here
    virtual void ShowArea() const;
    //other code here 
};
class Shape3D : virtual public Shape {
    public:
//------vvvvvvv------------------------------>virtual  added here        
        virtual void ShowVolume() const;
        //other code here
};
//other code here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文