【问题标题】:The type a must implement the inherited pure virtual method ba 类型必须实现继承的纯虚方法 b
【发布时间】:2013-02-12 20:01:24
【问题描述】:

我想要使用抽象类在 C++ 中模拟接口原型。但在 Eclipse IDE 中,我得到“这一行的多个标记 - 类型“处理程序”必须实现继承的纯虚方法 '处理程序::setNext'"

我的问题是为什么会这样?

处理程序.h

class Handler {
 public:

    virtual void setNext(Handler &next)  = 0;
    Handler();
    virtual ~Handler();
    virtual void process()  = 0;
 public:

    Handler *nextInChain;

};

处理程序.cpp

#include "Handler.h"
Handler::Handler(){
}
Handler::~Handler(){
}

Oracle.h

#include "Handler.h"
class Oracle : virtual public Handler {
 public:
    Oracle();
    virtual ~Oracle();
    virtual void process();
    virtual void setNext(Handler &next);
 private:

};

Oracle.cpp

#include "Oracle.h"

Oracle::Oracle(){
Handler AQUI;//AQUI I get Multiple markers at this line
             //- The type 'Handler' must implement the inherited pure virtual method 
 //'Handler::setNext'
}

Oracle::~Oracle(){
}

void Oracle::process(){
}

void Oracle::setNext(Handler &next){
}

【问题讨论】:

  • 你确定你需要虚拟继承吗?虚函数也适用于非虚继承。

标签: c++


【解决方案1】:

这是不正确的:

Handler AQUI;

你不能实例化一个抽象类。

您要做的是定义一个指向Handler 的指针,并为其分配来自子类的有效对象的地址,例如Oracle

【讨论】:

    猜你喜欢
    • 2016-06-18
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    相关资源
    最近更新 更多