【问题标题】:"Marked as override but does not override" Problem in OOP CodeOOP 代码中的“标记为覆盖但不覆盖”问题
【发布时间】:2023-01-27 19:45:48
【问题描述】:

我正在尝试在 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 类的相关代码的 sn-p。

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 形状上崩溃所以它不是那些特定类的问题。

【问题讨论】:

  • 您需要为showAreashowVolume添加virtual关键字,以便showAreashowVolume可以是虚拟成员函数。
  • “并且该功能在其原始定义中是虚拟的”-- 不,它不是(除非原始定义在看不见的Shape 类中)。打字错误?

标签: c++ oop inheritance multiple-inheritance virtual-inheritance


【解决方案1】:

Shape2D类中声明的函数ShowArea

void ShowArea() const;

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

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

是不正确的。

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

void ShowVolume() const;

它可能不会在派生类中被覆盖。

您需要在可以覆盖它们的基类中将函数声明为虚拟函数。

【讨论】:

    【解决方案2】:

    问题目前成员函数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
    

    【讨论】:

      【解决方案3】:

      从 qt5 迁移到 qt6 时出现此错误

      error: 'X' marked 'override', but does not override可以制作

      // somewidget.h
      
      #include <QWidget>
      
      class SomeWidget : public QWidget
      {
          Q_OBJECT
      
      protected:
      
          // error: ‘void SomeWidget::enterEvent(QEvent*)’ marked ‘override’, but does not override
          //void enterEvent(QEvent *event) override;
      
          // good:
          virtual void enterEvent(QEvent *event);
      }
      
      // somewidget.cc
      
      #include "somewidget.h"
      
      // error:
      //void SomeWidget::enterEvent(QEvent *event)
      
      // good:
      virtual void SomeWidget::enterEvent(QEvent *event)
      {
          // ...
      }
      

      docs 原始签名是

      virtual void enterEvent(QEnterEvent *event);
      

      另见Qt: enterEvent and leaveEvent doesnt work

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-29
        • 1970-01-01
        • 1970-01-01
        • 2015-02-09
        • 1970-01-01
        • 2017-07-09
        • 1970-01-01
        • 2012-06-30
        相关资源
        最近更新 更多