我已经读过其他有关此错误的文章,并且看到了一个普遍的问题,即对象类中有纯虚方法,但是我看不到任何虚方法。
错误消息是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/