Thread: templates & linked list

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    templates & linked list

    if anyone explain me why this code is not working and where I'm making mistakes:

    Code:
    #include <iostream>
    using namespace std;
    
    template <class T>
    struct Node
    {
    	T*data;
    	struct Node *next;
    };
    
    template <class T>
    class List
    {
    private:
    	struct Node *head;
    public:
    	List():head(0){}
    	~List(){/*appropriate clean up*/}
    	void insert_new(T);
    };
    template <class T>
    void List<T>::insert_new(T dat)
    {
    	Node *tmp=new Node;
    	tmp->data=new T;
    	*(tmp->data)=dat;
    	if(!head)
    	{
    		head=tmp;
    		return ;
    	}
    	tmp->next=head;
    	head=tmp;
    }
    
    int main()
    {
    	List<int> Head;
    	Head.insert_new(1);
    }
    Thanks very much!

  2. #2
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    You could make a start by telling us what exactly is not working and what the code was supposed to do in the first place.
    [code]

    your code here....

    [/code]

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Well, I'm trying to implement Linked list with any type of data, so I use template, but I got stuck on first function insert new element in the list. When I compile the code I get following error:

    at line:

    tmp->data=new T;
    error
    error C2440: '=' : cannot convert from 'int *' to ' *'
    in line:

    Node *tmp=new Node;

    error:
    error C2512: 'Node' : no appropriate default constructor available
    in the same line:
    error:
    error C2955: 'Node' : use of class template requires template argument list

    and so on. Compile by yourself and see.
    I fix this with the following code:
    Code:
    #include <iostream>
    using namespace std;
    template <class T>
    class List
    {
    private:
    	struct Node //struct node in class
    	{
    		T*data;
    		struct Node *next;
    	}*head;
    	
    public:
    	List():head(0){}
    	~List(){/*appropriate clean up*/}
    	void insert_new(T);
    	void show(List &l);
    };
    template <class T>
    void List<T>::insert_new(T dat)
    {
    	Node *tmp=new Node;
    	tmp->data=new T;
    	*(tmp->data)=dat;
    	tmp->next=NULL;
    	if(!head)
    	{
    		head=tmp;
    		return ;
    	}
    	tmp->next=head;
    	head=tmp;
    }
    template <class T>
    void List<T>::show(List &l)
    {
    		Node*tmp=l.head;
    		while(tmp !=NULL)
    		{
    			cout<<*(tmp->data)<<" ";
    			tmp=tmp->next;
    		}
    }
    
    int main()
    {
    	List<int> Head;
    	Head.insert_new(1);
    	Head.insert_new(2);
    	Head.insert_new(4);
    	Head.insert_new(2);
    	Head.show(Head);
    	return 0;
    }
    so just by moving struct Node in the class
    why the other way is not working?

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    why the other way is not working?
    Because Node is a templated struct so you must provide the type for instantiation.
    Code:
    Node *tmp=new Node;
    should be
    Code:
    Node<T> *tmp=new Node<T>;
    Otherwise, the compiler has no idea how you want to parameterize Node.

    It works when you nest the Node struct inside your List class because Node just uses the type parameter from List.

    Hope that helps.

    -tf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM