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!
/***********************************************************************
* 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";
}
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)