2017-09-24 67 views
-2

我已经阅读了一些关于这个错误的其他帖子,并看到了流行的问题是在对象类中有纯虚拟方法,但我没有任何我可以看到。 错误消息是executive.h:13:44: error: invalid new-expression of abstract class type ‘List<std::__cxx11::basic_string<char> >’不能声明字段'行政::历史'是抽象类型

这是executive.h

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; 

我也从我的编译器得到一个说明说t他的,但我不做,因为它说我的班级名称所在的路线。

list.h:11:7: note: because the following virtual functions are pure within ‘List<std::__cxx11::basic_string<char> >’: 
class List: public ListInterface<T>{ 
+0

这很明显,这不是实际的编译错误。如果您逐个比较基类中的抽象方法和子类中的实现,您会发现其中一个方法的签名不匹配。你的家庭作业是学习如何使用'override'关键字,它将以更明显的方式捕捉到这个常见错误。 –

回答

0

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

+0

非常感谢你解决了我的问题! –