c++简单程序实现中的继承和虚函数错误
C++ Inheritance and Virtual function error in simple program implementation
我正在学习c++,我正在尝试实现一个简单的c++程序来创建一个抽象类,如下所示:请解释为什么我得到这个错误后编译?请用简单的语言解释一下,我不是专业术语专家,还在学习面向对象编程概念。提前感谢(代码和错误如下)
代码:
/*
* 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)
问题如下:在shape
类中,您声明draw为不带参数:
virtual void Draw(void)=0;
而子类circle
和square
有draw
期望双精度:
void Draw(double radius)
在circle
中,我认为radius
(连同可能像center这样的东西)应该传递给构造函数,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
类也有同样的问题
Draw方法的参数不匹配
形状的:
virtual void Draw(void)=0;
在圆形和方形中:
void Draw(double radius)
如果你想使用虚方法,参数必须匹配
Shape中的方法应该是(不需要void)
class shape{
public:
virtual void Draw()=0;
};
当你声明Circle时,你需要一个Draw
方法(即没有半径位的方法)
相关文章:
- "error: no matching function for call to"构造函数错误
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 尝试将unique_ptrs推送到向量时使用纯虚拟函数错误
- C++ OpenCV 卡尔曼滤波器构造函数错误
- C++:用户定义的显式类型转换函数错误
- C++ 合并字符串以'system'函数错误
- C++ wmain 函数错误时使用 Unicode
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- C++ 中常量属性的初始化构造函数错误
- 线程 std::调用未知类型,无法专门化函数错误
- 表单显示对话框函数错误,并且不执行下面的语句
- 函数错误 C2059:语法错误:'>'不起作用
- STL向量上出现奇怪的复制构造函数错误
- C++ Visual Studio 重载函数错误:没有重载函数的实例与指定的类型匹配
- C++引用已删除函数错误
- 为什么通过带有文字编号的引用调用会出现"无匹配函数"错误?
- C++ 乘法定义的构造函数错误消息似乎错误
- 在 c++ 代码中将数组传递给函数错误
- FFMPEG avcodec_decode_video2函数错误代码
- 为什么我会收到转换函数错误