【C++】error: cannot declare variable ‘x’ to be of abstract type ‘xxx’
18734 ワード
エラーの原因
派生クラスには、すべてのベースクラスの虚関数が定義されていません.私の問題は主にベースクラスに虚関数にconstキーワードがあり、派生クラスで関数を定義するときにconstキーワードを付けるのを忘れ、ベースクラスの虚関数を上書きしなかったことです.
エラーの例
変更後
派生クラスには、すべてのベースクラスの虚関数が定義されていません.私の問題は主にベースクラスに虚関数にconstキーワードがあり、派生クラスで関数を定義するときにconstキーワードを付けるのを忘れ、ベースクラスの虚関数を上書きしなかったことです.
エラーの例
typedef string elemType;
class Stack1{
public:
virtual void push(elemType& a) =0;
virtual elemType pop() = 0;
virtual int size() const=0;
virtual bool empty() const = 0;
virtual bool full() const = 0;
virtual elemType peek() const = 0;
virtual void print() const = 0;
};
class LIFO_Stack:public Stack1{
public:
LIFO_Stack();
LIFO_Stack(vector<elemType> &s):_vec(s){top=s.size()-1;};
int size() {return _vec.size();} //---
bool empty() {return _vec.size();} //---
bool full() {return _vec.size()==max_size;} //---
void push(elemType& a);
elemType pop() {_vec.pop_back();return _vec[top];}
elemType peek() {return _vec[top];}
void print(){
cout<<" :"<<endl;
int i;
for(i = 0;i<_vec.size();i++)
cout<<this->_vec[i]<<" ";
cout<<endl;
}
protected:
int max_size = 1024;
int top;
vector<elemType> _vec;
};
変更後
typedef string elemType;
class Stack1{
public:
virtual void push(elemType& a) =0;
virtual elemType pop() = 0;
virtual int size() const=0;
virtual bool empty() const = 0;
virtual bool full() const = 0;
virtual elemType peek() const = 0;
virtual void print() const = 0;
};
class LIFO_Stack:public Stack1{
public:
LIFO_Stack();
LIFO_Stack(vector<elemType> &s):_vec(s){top=s.size()-1;};
int size() const {return _vec.size();} //---
bool empty() const {return _vec.size();} //---
bool full() const {return _vec.size()==max_size;} //---
void push(elemType& a);
elemType pop() {_vec.pop_back();return _vec[top];}
elemType peek() const{return _vec[top];}
void print()const{
cout<<" :"<<endl;
int i;
for(i = 0;i<_vec.size();i++)
cout<<this->_vec[i]<<" ";
cout<<endl;
}
protected:
int max_size = 1024;
int top;
vector<elemType> _vec;
};