[英]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.