Hi All,
I encoutering this problem when trying to compile my code.
list.h:10: error: ‘List’ is not a template
It seems like the problem comes from the declaration of my friend template class List in template class ListNode.
This my lis.h file
//-----------------------------------------------------------------------------
// class ListeNode
// contains the operations for class List
//-----------------------------------------------------------------------------
template <class T>
class ListNode{
template <class U>
friend class List;
public:
ListNode<T>(T & t, ListNode<T> * p)
:data(t),next(p){}
protected:
T data; //record the data
ListNode<T> * next; //pointeur to the next node
};
//-----------------------------------------------------------------------------
// class Liste
// dynamic array
//-----------------------------------------------------------------------------
template <class U>
class List{
public:
List():first(0){}
~List();
public:
void insert(U t);//includes a new data in the head of the list
void remove(U &t);//remove the first element of the list
int isEmpty() {return first = 0;}
void print();
protected:
ListNode<U> * first;
ListNode<U> * newNode(U & t, ListNode<U> * p)
{
ListNode<U> * q = new ListNode<U>(t,p);
return q;
}
};
#endif
this is the implementation of list.h
#include "list.h"
template <class U>
List::~List()
{
ListNode<U> * tmp;
for (ListNode<U> * p =first; p; ){
tmp = p;
p = p->next;
delete tmp;
}
first = 0;
}
template <class U>
void List<U>::insert(U t)
{
ListNode<U> * p = newNode(t, first);
first = p;
}
template <class U>
int List::remove(U & t)
{
if (isEmpty()) return 0;
t = first->data;
ListNode<U> * p = first;
first = first->next;
delete p;
return 1;
}
template <class U>
void List<U>::print()
{
for (listNode<U> * p=first; p; p=p->next)
cout << p->datat << "->";
cout << "*\n";
}
and this is my main function
#include <iostream.h>
#include <string>
#include "list.h"
int main(){
List<int> x;
x.insert(5);
x.insert(2);
x.insert(3);
x.insert(4);
x.print();
int y;
x.remove(y);
cout << "Removed: " << y << endl;
x.print();
return 0;
}
Is any one who can help?
thank you.
Guy