Thread: Stack errors using templates

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    155

    Stack errors using templates

    Hello, I am attempting to create a stack.h file using templates and a linklist in order to hold the desrired values I "push" into the stack, but unfortunately it does not like the way I am attempting the problem... any help here would be greatly appreciated:


    CONTENTS OF DRIVER FILE:
    Code:
    #include <iostream>
    #include "stack2.h"
    using namespace std;
    
    int main() {
    	stack<int> myStack;
    	myStack.push(7);
    	myStack.push(3);
    	myStack.push(3);
    	myStack.push(1);
    	
    	while (myStack.count()) {
    		cout<<myStack.pop() << endl;
    	}
    	
    	return 0;
    }

    CONTENTS OF STACK.H FILE

    Code:
    #include <iostream.h>
    
    
    template <typename T> 
    struct item {
    	T value;
    	item* ptr;
    };
    
    template <class T> class stack {
    public:
    	private:
    		int counter;
    		item* start;
    	
    	public:
    		stack();
    		
    		int count();
    		
    		void push(T newtop);
    		
    		T pop();
    			
    };
    
    template <typename T>
    
    	stack<T>::stack(){
    		counter = 0; 
    		start = NULL; 
    	}
    
    template <typename T>
    
    	int stack<T>::count(){ 
    		return counter; 
    	}
    
    
    template <typename T>
    
    void stack<T>::push(T newtop){
    			item* temp = new item;
    			
    			temp->value = newtop;  //you may have to fill in other values.
    			temp->ptr = start;
    			
    			start = temp;
    			
    			counter++;
    		}
    
    
    template <typename T>
    
    T stack<T>::pop(){
    
    			if (counter > 0){
    				counter--;
    				T popValue = start->value;
    				start = start->ptr;
    
    				return popValue;	
    			}
    			 
    			else{
    				return T(); //or whatever you want your error value to be
    			}
    			
    		}

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Iīm not sure about this but I donīt think you can mix
    Code:
    template <class T>
    and later use
    Code:
    template <typename T>
    I think it depend on the compiler you are using.

    What kind of error do you get, compile-error or is the logic wrong. If you could be more specific whatis wrong it would certenly be easier to help.

    P.S. Have you made a search here??? I sure that a complete (code)version of a stack can be found here.

    Ahh here it is http://cboard.cprogramming.com/showt...=Stack+Prelude

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    If you're using a template type you've got to specify the type

    item* ptr;

    item is a template. You need the type; something like -

    item<T>* ptr;

    Do this for other items. Add some memory de-allocation and remove the redundant <iostream.h> and you should be away.
    Joe

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    SHe uses an array-based one, however. I need a linklist one

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Joe, what do you mean?

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Wow, it worked. Changing all my items to item<T>s it worked. How did this work!? Ahh!

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Code:
    template <typename T> 
    struct item {
    	T value;
    	item<T>* ptr;
    };
    
    template <class T> class stack {
    public:
    	private:
    		int counter;
    		item<T>* start;
    	
    	public:
    		stack();
    		int count();
    		
    		void push(T newtop);
    		
    		T pop();
    			
    };
    
    template <typename T>
    
    	stack<T>::stack(){
    		counter = 0; 
    		start = NULL; 
    	}
    
    template <typename T>
    
    	int stack<T>::count(){ 
    		return counter; 
    	}
    
    
    template <typename T>
    
    void stack<T>::push(T newtop){
    			item<T>* temp = new item<T>;
    			
    			temp->value = newtop;  //you may have to fill in other values.
    			temp->ptr = start;
    			
    			start = temp;
    			
    			counter++;
    		}
    
    
    template <typename T>
    
    T stack<T>::pop(){
    
    			if (counter > 0){
    				counter--;
    				T popValue = start->value;
    				start = start->ptr;
    
    				return popValue;	
    			}
    			 
    			else{
    				return T(); //or whatever you want your error value to be
    			}
    			
    		}
    The finished stack code. How can this be written better without all the template <typename t> stuff everywhere, etc? I must optimize this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. My little stack library - what do you think?
    By hauzer in forum C Programming
    Replies: 6
    Last Post: 10-17-2008, 09:01 AM
  3. Stack Implementation and Exceptions
    By audinue in forum C Programming
    Replies: 4
    Last Post: 06-22-2008, 09:32 AM
  4. Stack problem - I've hit a wall!
    By miniwhip in forum C Programming
    Replies: 7
    Last Post: 11-14-2007, 03:05 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM