c++ - 无法声明字段 ‘Executive::history’为抽象类型

标签 c++ compiler-errors abstract-class

我已经读过其他有关此错误的文章,并且看到了一个普遍的问题,即对象类中有纯虚方法,但是我看不到任何虚方法。
错误消息是executive.h:13:44: error: invalid new-expression of abstract class type ‘List<std::__cxx11::basic_string<char> >’
这是执行人员。

private:
    string command="";
    List<string>* history = new List<string>();
    int currentPosition=0;

public:
    Executive(string filename);
    ~Executive();
    void navigateTo(string url);

    void forward();

    void back();

    string current() const;

    void copyCurrentHistory(List<string>& destination);

    void printHistory(); 

List.h
template <class T>
class List: public ListInterface<T>{    
private:
    int length;
    Node<T>* head= nullptr;

public:
    List();
    ~List();
    bool isEmpty() const;

    int getLength() const;

    void insert(int newPosition, const T& newEntry) ;

    void remove(int position) ;

    void clear();

    T getEntry(int position);

    void setEntry(int position, const T& newEntry);

    Node<T>* getNode(int postion);

而我的纯虚拟方法是listInterface.h的类
template<class T>
class ListInterface
{    
public:


     virtual ~ListInterface() {}
     virtual bool isEmpty() const = 0;
     virtual int getLength() const = 0;
     virtual void insert(int newPosition, const T& newEntry) = 0;
     virtual void remove(int position) = 0;
     virtual void clear() = 0;
     virtual T getEntry(int position) const = 0;
     virtual void setEntry(int position, const T& newEntry) = 0;

我的编译器也发出了这样的注释,但我没有这样做,因为它说的是我的类名所在的行。
list.h:11:7: note:   because the following virtual functions are pure within ‘List<std::__cxx11::basic_string<char> >’:
 class List: public ListInterface<T>{

最佳答案

T getEntry(int position);不是virtual T getEntry(int position) const = 0;的实现。将const添加到方法的签名中。

关于c++ - 无法声明字段 ‘Executive::history’为抽象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46395465/

相关文章:

c++ - 使用 C++ 获取本地管理员用户名

c++ - 重载运算符拼图 : return *this, 还是临时变量?

java - 有没有更好的方法来创建一个扩展另一个抽象类并添加新功能的抽象类?

.net - 在非托管代码中处理托管委托(delegate)

c++ - 检查 istreambuf_iterator 失败

Java递归构建错误: array required,但找到字符串?

Go 编译器 : error line number is incorrect giving 1048575 i. e. 0xFFFFF 当问题在文件中更进一步时

dictionary - 为什么这个字典功能不起作用

java - 在Java中,是否可以要求接口(interface)的实现来覆盖对象方法?

c++ - 分层接口(interface)和实现