C++ 错误:不允许抽象类类型的对象:纯虚函数没有覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26825194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:40:25  来源:igfitidea点击:

C++ error: object of abstract class type is not allowed: pure virtual function has no overrider

c++

提问by Bacu

Having trouble with inheritance. I have no idea what I'm doing wrong.

继承有问题。我不知道我做错了什么。

FigureGeometry.h

FigureGeometry.h

    #ifndef FIGUREGEOMETRY
#define FIGUREGEOMETRY

static const float PI = 3.14159f;

class FigureGeometry
{

public:
    virtual float getArea() const = 0;
    virtual float getPerimeter() const = 0;
};

#endif

Circle.h

圆.h

    #ifndef CIRCLE
#define CIRCLE

#include "FigureGeometry.h"

class Circle:public FigureGeometry
{
    float radius;
public:
    Circle(float theRadius)
    {
        radius = theRadius;
    }
    float getRadius() {return radius;}
    float getArea() {return getRadius() * getRadius() * PI;}
    float getPerimeter() {return getRadius() * 2 * PI;}
};

#endif

and then in main.cpp, on the line containing "Circle c1(5);"I get the error:

然后在 main.cpp 中,在包含"Circle c1(5);"我得到错误的行上:

21  IntelliSense: object of abstract class type "Circle" is not allowed:
            pure virtual function "FigureGeometry::getArea" has no overrider
            pure virtual function "FigureGeometry::getPerimeter" has no overrider   c:\Users\moog\Documents\Visual Studio 2012\Projects\data structures 3\data structures 3\main.cpp    9   9   data structures 3

回答by ravi

Your functions should be:-

您的职能应该是:-

float getArea() const {return getRadius() * getRadius() * PI;}
float getPerimeter() const {return getRadius() * 2 * PI;}

REASON FOR THIS BEHAVIOR :-

这种行为的原因:-

When you re-define a function in derived class with same parameters as in base class then that's called as overriding. Whereas if you re-define that function with different parameter then it would be an attempt to use overloading from you side. But overloading is possible only in class scope. So, in this case corresponding base class function would be hidden.

当您在派生类中使用与基类中相同的参数重新定义函数时,这称为覆盖。而如果您使用不同的参数重新定义该函数,那么这将是尝试从您这边使用重载。但是重载只能在类范围内进行。因此,在这种情况下,相应的基类函数将被隐藏。

For e.g:- Below is futile attempt on overloading.

例如:- 下面是重载的徒劳尝试。

class Base
{
public:
   virtual void display () const;
};

class Derived 
{
public:
   virtual void display ();
};

int main()
{
    const Derived d;
    d.display();           //Error::no version defined for const....
}

So you are getting error as display in derived would hide display in base.

所以你会得到错误,因为派生中的显示会隐藏基础中的显示。

Similarly your pure virtual function would be hidden i.e compiler treat that case as there is no function defined in derived corresponding to base class pure virtual function.That would make derived also a abstract class.

同样,您的纯虚函数将被隐藏,即编译器会处理这种情况,因为派生中没有定义对应于基类纯虚函数的函数。这将使派生也是一个抽象类。

Hope things are crystal clear...

希望事情很清楚...

回答by Vlad from Moscow

You forgot to place qualifier constfor these virtual functions in the derived class. Write

您忘记const在派生类中放置这些虚函数的限定符。写

float getArea() const {return getRadius() * getRadius() * PI;}
float getPerimeter() const {return getRadius() * 2 * PI;}

So in fact in the derived class you declared new functions that hide virtual functions in the base class with the same name.

所以实际上在派生类中你声明了新函数,这些函数隐藏了基类中同名的虚函数。

Also you should to declare the destructor also as virtual. For example

此外,您还应该将析构函数声明为虚拟的。例如

class FigureGeometry
{
public:
    // ...
    virtual ~FigureGeometry() = default;
};

Or

或者

class FigureGeometry
{
public:
    // ...
    virtual ~FigureGeometry() {}
};

回答by 0x499602D2

Your getArea()and getPerimeter()methods do not override the virtual methods declared in FigureGeometrybecause they do not match in const-qualification. Therefore Circlebecomes abstract since those methods were pure virtual; and you can't create objects of abstract type.

您的getArea()getPerimeter()方法不会覆盖 in 中声明的虚拟方法,FigureGeometry因为它们在const-qualification 中不匹配。因此Circle变得抽象,因为这些方法是纯虚拟的;并且您不能创建抽象类型的对象。

To fix this, simply add constto both of your methods. Also, to make sure you have correctly overrided methods from the base class, use the overridespecifier:

要解决此问题,只需添加const到您的两种方法中即可。此外,为了确保您正确覆盖了基类中的方法,请使用说明override符:

float getArea() const override;
float getPerimeter() const override;

If they do not override the compiler will let you know with an error message.

如果它们不覆盖,编译器将通过错误消息通知您。