templates giving me problems

This is a discussion on templates giving me problems within the C++ Programming forums, part of the General Programming Boards category; Hi, I made a stack class for myself for ints, now want to template it, but I get wierd syntax ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    85

    templates giving me problems

    Hi, I made a stack class for myself for ints, now want to template it, but I get wierd syntax errors, but I can't see whats wrong with it.

    Code:
    struct Node;
    typedef Node* NodePtr;
    
    
    template<class T>
    class List
    {
    	public:
    		List();
    		void Push(T i); // Pushes i onto stack
    		T Pop(); // Pops top element from stack
    		int IsEmpty(); // returns 1 if empty; 0 if not empty;
    	private:
    		struct Node
    		{
    			T Item;
    			NodePtr Next;
    		};
    		NodePtr Head;
    };
    
    
    
    List::List()
    {
    	Head = 0;
    }
    
    template<class T>
    void List<T>::Push(T i) // Pushes i onto stack
    {
    	NodePtr tmp,curr;
    	
    	//create new node and store integer
    	tmp = new Node;
    	tmp->Item = i;
    	tmp->Next = 0;
    	
    	//add to top of stack if not empty
    	if(Head != 0)
    	{
    		curr = Head;
    		while(curr->Next)
    			curr = curr->Next;
    		curr->Next = tmp;
    	}
    	else
    		Head = tmp;
    }
    
    template<class T>
    T List<T>::Pop() // Pops top element from stack
    {
    	T tmp;
    	
    	if(Head == 0) //nothing to pop!
    		return 0;
    	
    	tmp = Head->Item;
    	Head = Head->Next;
    	
    	return tmp;
    }
    
    int List::IsEmpty() // returns 1 if empty; 0 if not empty;
    {
    	if(Head == 0) 
    		return 1;
    	return 0;
    }

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    first, don't post complaing of "weird syntax errors" unless you post the errors!

    second, don't typedef pointer types. it confuses clients of your class

    third, you should initialise Head in the initialiser list of your constructor (that's just good style)
    Code:
    List::List()
    : Head(0)
    {
    }
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Ok,
    syntax error before :: token at List::List....
    I can't paste my errors im using quincy, im sure its a silly mistake

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    ooops silly me. Found the error. I like typedefs to get rid of the stars for pointers, had trouble with the forward declaration of struct but it works now.

    Code:
    template<class T>
    class List
    {
    	public:
    		List();
    		void Push(T i); // Pushes i onto stack
    		T Pop(); // Pops top element from stack
    		int IsEmpty(); // returns 1 if empty; 0 if not empty;
    	private:
    		struct Node
    		{
    			T Item;
    			Node *Next;
    		};
    		Node *Head;
    };
    
    
    template <class T>
    List<T>::List()
    {
    	Head = 0;
    }
    
    template<class T>
    void List<T>::Push(T i) // Pushes i onto stack
    {
    	Node *tmp, *curr;
    	
    	//create new node and store integer
    	tmp = new Node;
    	tmp->Item = i;
    	tmp->Next = 0;
    	
    	//add to top of stack if not empty
    	if(Head != 0)
    	{
    		curr = Head;
    		while(curr->Next)
    			curr = curr->Next;
    		curr->Next = tmp;
    	}
    	else
    		Head = tmp;
    }
    
    template<class T>
    T List<T>::Pop() // Pops top element from stack
    {
    	T tmp;
    	
    	if(Head == 0) //nothing to pop!
    		return 0;
    	
    	tmp = Head->Item;
    	Head = Head->Next;
    	
    	return tmp;
    }
    
    template <class T>
    int List<T>::IsEmpty() // returns 1 if empty; 0 if not empty;
    {
    	if(Head == 0) 
    		return 1;
    	return 0;
    }

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    well in general template class methods defined outside the class definition should look like this

    Code:
    template <class T>
    List<T>::methodName()
    {
    }
    try rewriting your constructor and IsEmpty() methods with that syntax
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. learning Templates.. having problems..
    By MegaManZZ in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2008, 04:53 PM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. DDraw Problems
    By Unregistered in forum Game Programming
    Replies: 16
    Last Post: 06-06-2002, 06:39 AM

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