类型a必须实现继承的纯虚拟方法b

The type a must implement the inherited pure virtual method b

本文关键字:虚拟 方法 继承 实现 类型      更新时间:2023-10-16

我想要使用抽象类在C++中模拟接口sterotype。但是在Eclipse IDE中,我得到"这一行有多个标记-类型"Handler"必须实现继承的纯虚拟方法'处理程序::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){
}

这是不正确的:

Handler AQUI;

不能实例化抽象类。

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