【问题标题】:‘class shape’ has no member named ‘info’ but adding one doesn't work either“类形状”没有名为“信息”的成员,但添加一个也不起作用
【发布时间】:2014-03-28 12:09:38
【问题描述】:

我正在尝试编译一些代码 (This code),但是当我注释掉第 25 行时:
virtual void info()=0;
它不编译:

shape.cpp: In function ‘int main()’:
shape.cpp:345:11: error: ‘class shape’ has no member named ‘info’
  svec[0]->info();

但是保留第 25 行对于纯虚函数会产生很长的错误...

shape.cpp:77:15: error: cannot declare parameter ‘squ’ to be of abstract type ‘square’
   cube(square squ):
               ^
shape.cpp:30:7: note:   because the following virtual functions are pure within ‘square’:
 class square : public shape {
       ^
shape.cpp:25:16: note:  virtual void shape::info()
   virtual void info()=0;
                ^
shape.cpp:167:20: error: cannot declare parameter ‘rec’ to be of abstract type ‘rectangle’
   cuboid(rectangle rec, double d):
                    ^
shape.cpp:110:7: note:   because the following virtual functions are pure within ‘rectangle’:
 class rectangle : public shape {
       ^
shape.cpp:25:16: note:  virtual void shape::info()
   virtual void info()=0;

等等……

谁能告诉我我做错了什么?谢谢。

【问题讨论】:

    标签: c++ oop inheritance polymorphism abstract-base-class


    【解决方案1】:

    函数在派生类中声明为const,而不是在基类中。这意味着派生类不会覆盖该函数;他们只是声明了一个同名的不同函数。

    在基类中添加const,或者在派生类中删除。

    【讨论】:

    • 谢谢迈克,解决了问题!我会尽快接受你的回答
    猜你喜欢
    • 2018-12-31
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    相关资源
    最近更新 更多