2011-11-30 343 views
2

有没有人知道我在做什么错误,并向我解释为什么它不让我声明任何东西为圆形? 主要错误:无法将变量'c'声明为抽象类型'circle'

int main(void){ 
... 
    circle c; 
... 
} 

circle.h

#include <string> 
#include <iostream> 
using namespace std; 

class circle : public shape { 

    double diameter, circum, radius; 

public: 

    virtual void draw(){ 
     cout<< "Circle"<< endl; 
    } 
}; 

shape.h

#include <string> 
#include <iostream> 

using namespace std; 

class shape{ 

public: 
    virtual void draw() const = 0; 
}; 
+3

你忘了'const' –

回答

8
virtual void draw() const { 
    cout<< "Circle"<< endl; 
} 

您应该添加const关键字像上面的例子。

4

你的shape类中的绘图函数被声明为const,而你的circle类中的绘图函数不是。因此,圈内的人不会覆盖形状。所以它仍然是抽象的,因为它没有重载纯虚函数。

2

你在循环绘制的定义后缺少一个const。

virtual void draw() const { cout<<"Circle"<<endl; } 

当使用抽象函数,其函数原型/签名具有相匹配-exactly-

编辑:Blegh,通过30秒殴打。

-1

如果你懒得看你编译器的输出像这样的显示:

g++ main.cpp 
main.cpp: In function ‘int main()’: 
main.cpp:5:12: error: cannot declare variable ‘c’ to be of abstract type ‘circle’ 
circle.h:6:29: note: because the following virtual functions are pure within ‘circle’: 
shape.h:9:18: note:  virtual void shape::draw() const