i am trying to write a Stack class with template. but i've got 8 errors like this
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall stack<int>::stack<int>(int)" (??0?$stack@H@@QAE@H@Z) referenced in function _main
with every function mentioned.
But when i tried not to use template, rewrote this code with int, it worked properly.
#ifndef STACK_H
#define STACK_H
template <class T>
class stack{
T *S;
int maxSize, Top;//Top- index of the last elem
public:
stack(int n=100);
~stack();
void push(T&);
void pop();
T& top();
bool empty();
bool full();
int size();
int maxsize();
void clear();
};
#endif
When instantiating a templated object, the entire definition of the template class has to be known at compile time. This means that you have to include the entire definition of the template and its methods in the header file, rather than putting the method definitions in a separate .cpp file.
(There are other ways of dealing with this, but putting everything in the header is the standard way, for various reasons).