templates & linked list

This is a discussion on templates & linked list within the C++ Programming forums, part of the General Programming Boards category; if anyone explain me why this code is not working and where I'm making mistakes: Code: #include <iostream> using namespace ...

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

    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
    712
    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, 03:09 AM
  2. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 12:56 PM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 08:27 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21