I'm trying to implement a data structure that inherits from an abstract type the List in question. I'm following the guide from The Tour Of C++ book. Unfortunately my code doesn't compile I went over similar questions but the problem is different I only use one .cc file no headers nothing just a simple test .
template <class T>
class List {
public:
virtual int size()=0;
virtual T get(int)=0;
virtual void set(int,T)=0;
virtual void add(T)=0;
virtual void remove(int)=0;
};
template<class T>
class ArrayStack : public List {
public:
ArrayStack(){
len = 0;
}
void add(T e){
a.push_back(e);
len++;
}
T get(int idx){
return a[idx];
}
void set(int idx,T e){
a[idx] = e;
}
int size(){
return len;
}
void remove(int idx){
a.erase(a.begin()+idx);
}
~ArrayStack(){}
private:
std::vector<T> a;
int len;
};
template <class T>
void print_list(List<T>& l){
for(auto i=0;i < l.size();i++){
std::cout << l.get(i) << std::endl;
}
}
int main(){
ArrayStack<int> m;
m.add(1);
m.add(2);
m.add(3);
print_list(m);
}
The compilation error :
list.cc:28:32: error: expected class-name before ‘{’ token
class ArrayStack : public List {
Aucun commentaire:
Enregistrer un commentaire