I have a node struct and stack class. When I put the definition for ‘void Push(T data)’ outside the class definition I get:
error: 'template<class T> class Stack' used without template parameters
But when I put it inside the class definition it works fine.
Here is the code:
template <class T>
struct Node
{
Node(T data, Node <T> * address) : Data(data), p_Next(address) {}
T Data;
Node <T> * p_Next;
};
template <class T>
class Stack
{
public:
Stack() : size(0) {}
void Push(T data);
T Pop();
private:
int size;
Node <T> * p_first;
Node <T> * p_last;
};
The implementation for Push(T data) is :
void Stack::Push(T data)
{
Node <T> * newNode;
if(size==0)
newNode = new Node <T> (data, NULL);
else
newNode = new Node <T> (data, p_last);
size++;
p_last = newNode;
}
Edit: The solutions worked except that now I get a linking error whenever I try to call the functions.
Stack<int>::Pop", referenced from
_main in main.o
symbol(s) not found.
unless the definitions are in Stack.h instead of Stack.cpp
You need to use the
template <class T>again (and then use thatTagain as the template parameter for the class):