Notices
Results 1 to 2 of 2

Thread: C++ Template Question regarding a Linked List.

  1. #1 C++ Template Question regarding a Linked List. 
    Forum Freshman
    Join Date
    Nov 2009
    Posts
    9
    My question is about C++ templates. I understand how to use them when the entire class and methods will be using one data type. My problem is occurring when using multiple data types such as nodes and say ints or floats. For example:
    Code:
    template<class Type>
    struct Node
    {
    	Type data;
    	Node* next;
    	Node* tail;
    };
    
    template<class Type>
    class dbList
    {
    private:
    
    	int size;
    	Node* head;
    	Node* tail;
    	
    public:
    	
    	dbList()
    	{
    		size=0;
    		head=0;
    		tail=0;
    	}
    	
    };
    For the code above the node structs generic type will be the data it holds. My compiler,xcode, is complaining about the use of Nodes as a data type. My questions are: how would I code this to be a template but make it where it only uses the data as a generic type.
    My second question is how to incorporate this into using the class "dbList" as well?

    The only thing I can think of is to define another template class for node and for the class "dblist"? It just seems that there is an easier way and that what I'm thinking would cause a lot of problems. Thank you for your time.


    Reply With Quote  
     

  2.  
     

  3. #2  
    Forum Bachelors Degree
    Join Date
    Mar 2009
    Posts
    421
    Node is a template struct, so I think you have to instantiate it. So in the private section of class dbList, try writing Node<Type> *next, etc.


    Reply With Quote  
     

Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •