Help with making a template

I'm trying to convert a class I wrote to a template class. I've never used templates before so I'm not sure how to fix the errors I'm getting. My program calls the class in from a header file and I use another file to define my class functions.

The two functions I posted have the compile errors I'm having trouble with. It looks like they should be easy fixes but I can't seem to work it out.

Any help on what I need to do to fix these problems is much appreciated!

This is the header file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/***********************************************************************                          
 * Name: Stack Class                                                                              
 * Desc: Holds all the variables and functions to run a stack.                                    
 ***********************************************************************/
template <class T>                                                                              
class Stack                                                                                       
{                                                                                                 
   private:                                                                                       
     int myTop;                  // index of top of stack                                         
     int maxSize;                // maximum size of stack                                         
     T   *stackItems;            // pointer for dynamic array                                     
   public:                                                                                        
     Stack     (int size = 100); // default constructor                                                                 
     Stack     (const Stack<T> &aStack);  // copy constructor                                                         
     T    top  (void);           // returns the value of top item in the stack                    
     void push (T item);         // store item on the stack                                       
     void pop  (void);           // remove/delete top item off stack                              
     bool empty(void);           // is stack empty                                                
     bool full (void);           // is stack full                                                 
     ~Stack();                   // Destructor                                                        
     Stack<T>& operator = (const Stack<T> &rtSide); // overloaded = operator                                               
};


Here's one of the functions I'm getting compile errors on.

stack.cpp:44: error: expected constructor, destructor, or type conversion before ‘<’ token
1
2
3
4
5
6
7
8
9
10
11
/***********************************************************************                          
 * Name: Stack                                                                                    
 * Desc: Default Constructor that sets the stack size to 100                                      
 ***********************************************************************/
template <class T>
Stack<T>::Stack(int size)     /*** COMPILE ERROR line 44 ***/
{
   myTop = 0;                                                              
   maxSize = size;                                                             
   stackItems = new T[maxSize];                                      
}


Here's another function with compile errors.

stack.cpp:74: error: expected initializer before ‘<’ token
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/***********************************************************************                          
 * Name: Push                                                                                     
 * Desc: Store item on the stack                                                                  
 ***********************************************************************/
template <class T>
void Stack<T>::push(T item)  /*** COMPILE ERROR line 74***/
{
   if (full())
   {
      cout << "ERROR: Stack Overflow" << endl;
      exit(1);
   }
   else
   {
      stackItems[myTop] = item;
      myTop++;
   }
}


I'm just using main to test a few of my template functions to see if they work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*  main program tests Push and Full functions by pushing 6 items on a                            
 *  stack of 5.                                                                                   
 */
main()   
{
   Stack<int> st(5);
   int data[5] = {1,2,3,4,5};
   int i;

   cout << "TESTING PUSH FUNCTION:" << endl;
   for (i = 0; i < 5; ++i)
      st.push(data[i]);
   cout << "TESTING FULL FUNCTION: Inserting a 6th item" << endl;
   cout << "This function should fail and leave error message" << endl;
   st.push(-1);
   cout << "Test Completed\n";
}
Last edited on
For example in the code
1
2
3
4
5
6
7
8
9
10
11
/***********************************************************************                          
 * Name: Stack                                                                                    
 * Desc: Default Constructor that sets the stack size to 100                                      
 ***********************************************************************/
template <class T>
Stack<T>::Stack(int size)     /*** COMPILE ERROR line 44 ***/
{
   myTop = 0;                                                              
   maxSize = size;                                                             
   stackItems = new T[maxSize];                                      
} 

write

Stack<T>::Stack<T>(int size)
That's it? So whenever I have a my class named anywhere it needs <T> in front of it? Thanks for the quick answer :)
Yep. Also, calling Stack (making objects from it, for instance), we need Stack</*Insert the typename you want T to be here*/>
I don't think vlad is right on this one...
Stack<T>::Stack(int size) should be fine. One nuance of templates is you can't truly have the implementation in a separate location from the header, you need to actually include the implementation file IN the header file if you insist on having them in separate files. Are you not doing this by chance?
No, i'm not including the implementation with the header file. I should have been more clear on that when I first posted. I'll throw them all together then and see what happens.
Now I'm confused on whether I need to use void Stack<T>::Stack<T>(int size) or void Stack<T>::Stack(int size)
Last edited on
The latter - just include the .cpp (or hpp, if you want to name it such) in the .h, and you should be good.
Alright, Thanks for the help everyone!
Topic archived. No new replies allowed.