首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单程序实现中的C++继承与虚函数错误

简单程序实现中的C++继承与虚函数错误
EN

Stack Overflow用户
提问于 2014-03-09 17:10:53
回答 3查看 617关注 0票数 0

我正在学习C++,我正在尝试实现一个简单的c++程序来创建一个抽象类,如下所示:请解释为什么编译后会出现这个错误?请用简单的语言来解释,我不是术语专家,而且还在学习OOPS的概念。预先谢谢(代码和错误)

代码:

代码语言:javascript
复制
/*
 * 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);
    }
 }

错误:

代码语言:javascript
复制
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)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-03-09 17:15:51

问题如下:在shape类中,您声明了不接受参数的绘制:

代码语言:javascript
复制
virtual void Draw(void)=0;

而子类circlesquaredraw需要一个double:

代码语言:javascript
复制
void Draw(double radius)

circle中,我认为radius应该传递给构造函数(可能还有类似中心的东西),而绘图不应该接受任何东西。例如:

代码语言:javascript
复制
class circle:public shape{
private:
    double radii;
public:
    circle(double radius) radii(radius) {};
    void Draw(){
        cout << "The radius of the circle is "  << radii<<endl;
    }
};

然后你用它作为

代码语言:javascript
复制
circle *p = new circle(radius);
p->Draw();

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

代码语言:javascript
复制
circle c(radius);
c.Draw()

当然,square类也有同样的问题。

票数 2
EN

Stack Overflow用户

发布于 2014-03-09 17:13:06

绘制方法的参数不匹配。

形状:

代码语言:javascript
复制
virtual void Draw(void)=0;

在圆圈和正方形中:

代码语言:javascript
复制
void Draw(double radius)

如果您想拥有虚拟方法,则参数必须匹配。

票数 1
EN

Stack Overflow用户

发布于 2014-03-09 17:15:01

这种方法的形状应该是(不需要空的)。

代码语言:javascript
复制
class shape{ 
public: 
    virtual void Draw()=0;
};

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22285261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档