friends template

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Guy Lusilao
    New Member
    • Oct 2006
    • 3

    friends template

    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
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I expect the problem is at the line

    friend class List;

    although you have not clearly indicated this.

    This is because when ListNode is declared there has not yet been a declaration of List. However you can not move the declaration of list before the declaration of ListNode because List uses ListNode (well you could but it wouldn't get rid of the problem just move it to a different line).

    What you need to do is forward declare the class List. A forward declaration tells the compiler that there will be a class (or structure or enum or union) of a given type but it is not declared yet. This provides enough information for the compiler to create pointers to that class or use it in a friend statement. A forward declaration looks like this

    template <class U>
    class List;

    Put it in the header before ListNode is declared.

    Comment

    Working...