简体   繁体   English

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

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

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 Circle.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);" 然后在main.cpp中,在包含"Circle c1(5);" I get the error: 我收到错误:

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

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 eg:- 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. 所以你得到错误,因为派生中的显示会隐藏在base中的显示。

Similarly your pure virtual function would be hidden ie 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... 希望事情很清楚......

You forgot to place qualifier const for these virtual functions in the derived class. 您忘记在派生类中为这些虚函数放置限定符const Write

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() {}
};

Your getArea() and getPerimeter() methods do not override the virtual methods declared in FigureGeometry because they do not match in const -qualification. 您的getArea()getPerimeter()方法不会覆盖在FigureGeometry声明的虚方法,因为它们在const FigureGeometry不匹配。 Therefore Circle becomes abstract since those methods were pure virtual; 因此, Circle变得抽象,因为这些方法是纯虚拟的; and you can't create objects of abstract type. 而且你不能创建抽象类型的对象。

To fix this, simply add const to both of your methods. 要解决此问题,只需在两个方法中添加const即可。 Also, to make sure you have correctly overrided methods from the base class, use the override specifier: 另外,要确保从基类中正确覆盖了方法,请使用override说明符:

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

If they do not override the compiler will let you know with an error message. 如果它们没有覆盖,编译器将通过错误消息通知您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ object of abstract class type "mazeGenerator" is not allowed: pure virtual function "olcGameEngine::OnUserUpdate" has no overrider - C++ object of abstract class type "mazeGenerator" is not allowed: pure virtual function "olcGameEngine::OnUserUpdate" has no overrider 错误消息:不允许抽象类类型为“ X”的对象:纯虚拟“ Y”没有替代程序 - Error message: Object of abstract class type “X” is not allowed: Pure virtual “Y” has no overrider C++ 纯虚拟 function 没有覆盖 - C++ pure virtual function has no overrider 抽象类类型…不允许,不能覆盖纯虚函数 - abstract class type … is not allowed, pure virtual function is not override C++ 错误:不允许抽象类类型的对象 - C++ error: object of abstract class type is not allowed C++ - 不允许抽象类类型的对象 - C++ - Object of abstract class type is not allowed 带有纯虚运算符+函数的C++抽象基类 - C++ Abstract Base Class with pure virtual operator+ function C ++中的抽象类和纯虚方法 - Abstract Class and Pure Virtual Method in C++ 不允许使用抽象类类型“cv :: BackgroundSubtractorMOG2”的对象。所有方法都是纯虚拟的 - Object of abstract class type “cv::BackgroundSubtractorMOG2” is not allowed. all the methods are pure virtual 不允许使用抽象类类型为“动物”的对象:(C ++) - Object of abstract class type “Animal” is not allowed: (C++)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM