“标记为覆盖但不覆盖” OOP 代码中的问题
我正在尝试用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Shape2D
类中声明的函数ShowArea
不是虚函数。所以派生类
Square
中的这个声明是不正确的。
此外,类
Shape3D
中声明的函数ShowVolume
不是虚函数,它不能在派生类中被重写。
您需要在基类中将函数声明为虚拟函数,以便可以重写它们。
The function
ShowArea
declared in the classShape2D
is not a virtual function. So this declaration in the derived class
Square
is incorrect.
Also the function
ShowVolume
declared in the classShape3D
is not a virtual functionIt 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.
问题是目前成员函数
showArea
和showVolume
不是虚拟成员函数,我们可以使用override
关键字仅在重写虚拟成员函数时。要解决这个问题,您需要通过添加关键字
virtual
来创建showArea
和showVolume
虚拟成员函数,如下所示:The problem is that currently the member functions
showArea
andshowVolume
are not virtual member functions and we can useoverride
keyword only when overriding a virtual member function.To solve this you need to make
showArea
andshowVolume
virtual member functions by adding the keywordvirtual
as shown below: