【问题标题】:C++ Inheritance and Virtual function error in simple program implementation简单程序实现中的 C++ 继承和虚函数错误
【发布时间】:2014-03-09 17:10:53
【问题描述】:

我正在学习 C++,我正在尝试实现一个简单的 C++ 程序来创建一个抽象类,如下所示: 请解释为什么我在编译后会收到此错误?请用简单的语言解释我不是术语专业人士,但仍在学习 OOPS 概念。提前致谢(代码和错误如下)

代码:

/*
 * shape.cpp
 *
 *  Created on: Mar 9, 2014
 *      Author: Drix
*/


#include <iostream>
using namespace std;

class shape{
public:
virtual void Draw(void)=0;
};


class circle:public shape{
public:
void Draw(double radius){
    radii = radius;
    cout << "The radius of the circle is "  << radii<<endl;
}
private:
double radii;
};

class square:public shape{
public:
void Draw(double side){
    s = side;
    cout << "The length of side of sqare is "  << s<<endl;
}
private:
double s;
};


int main(){

cout <<"Welcome to shape drwaing program"<<endl;
cout <<"Enter 1 to draw a sqare or 2 to draw a circle"<<endl;
int input;

cin>>input;
if(input == 1)
{
    cout << "Please enter the radius of the circle: ";
    double radius;
    cin >> radius;
    circle *p = new circle;
    p->Draw(radius);
}
if(input == 2)
    {
        cout << "Please enter the length of the side of a square: ";
        double side;
        cin >> side;
        square *t = new square;
        t->Draw(side);
    }
 }

错误:

10:58:15 **** Incremental Build of configuration Debug for project Shape ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o shape.o "..\\shape.cpp" 
..\shape.cpp: In function 'int main()':
..\shape.cpp:51:19: error: cannot allocate an object of abstract type 'circle'
..\shape.cpp:18:7: note:   because the following virtual functions are pure within 'circle':
..\shape.cpp:14:15: note:   virtual void shape::Draw()
..\shape.cpp:59:20: error: cannot allocate an object of abstract type 'square'
..\shape.cpp:28:7: note:   because the following virtual functions are pure within 'square':
..\shape.cpp:14:15: note:   virtual void shape::Draw()

10:58:16 Build Finished (took 884ms)

【问题讨论】:

  • 简而言之,void Draw(void)void Draw(double) 不一样,所以你并没有覆盖基类的纯虚方法。那里有很多重复项。

标签: c++ class inheritance abstract


【解决方案1】:

问题如下:在 shape 类中,您声明 draw 不带参数:

virtual void Draw(void)=0;

而子类 circlesquaredraw 期望双:

void Draw(double radius)

circle 中,我认为radius(可能连同中心之类的东西)应该传递给构造函数并且draw 不应该接受任何内容。例如:

class circle:public shape{
private:
    double radii;
public:
    circle(double radius) radii(radius) {};
    void Draw(){
        cout << "The radius of the circle is "  << radii<<endl;
    }
};

然后你把它当作

circle *p = new circle(radius);
p->Draw();

或者如果您不需要动态分配:

circle c(radius);
c.Draw()

当然square班级也有同样的问题。

【讨论】:

  • 谢谢,我发现这种优化非常有帮助circle *p = new circle(radius); 单行。当然,我也可以将参数传递给构造函数。
【解决方案2】:

Draw 方法的参数不匹配。

形状:

virtual void Draw(void)=0;

在圆形和方形中:

void Draw(double radius)

如果你想要虚拟方法,参数必须匹配。

【讨论】:

  • 啊!谢谢.. 跟进问题:有没有办法覆盖这种行为?表示 Shape(基类)参数中的虚函数没有类型,但派生类能够传递任何类型的参数 double 或 char 或 int。
【解决方案3】:

Shape中的这个方法应该是(不需要void)

class shape{ 
public: 
    virtual void Draw()=0;
};

当您声明 Circle 时,您需要一个 Draw 方法(即没有半径位的方法)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2015-08-23
    • 1970-01-01
    相关资源
    最近更新 更多