Hello, I'm using VisualC++ version 6.0 to make a console application.
Get the following error:
sandbox.obj : error LNK2001: unresolved external symbol "public: __thiscall Stack<int>::~Stack<int>(void)" (??1?$Stack@H@@QAE@XZ)
Actuall, get 6 similar errors, one for each member function.
If I cut and paste stack.cpp code directly into sandbox.cpp everything works fine. And stack.h and stack.cpp are in the project.
Here's the code:
sandbox.cpp
Code:
#include "../Libraries/include/stack.h"
int main() {
Stack<int> s;
return 0;
}
=======================
stack.h
Code:
template<class T> class Stack {
private:
struct StackNode {
T data;
StackNode * next;
StackNode( const T& newData,
StackNode *nextNode ):
data( newData ), next( nextNode ) {}
};
StackNode * top;
int size;
public:
Stack();
~Stack();
int getSize() const;
void push( const T& object );
T pop();
};
==============================
stack.cpp
Code:
#include "include/stack.h"
template<class T>
Stack<T>::Stack(): top( 0 ), size( 0 ) {}
template<class T>
Stack<T>::~Stack() {
while( top ) {
StackNode * toDie = top;
top = top->next;
delete toDie;
}
}
template<class T>
int Stack<T>::getSize() const {
return size;
}
template<class T>
void Stack<T>::push( const T& object ) {
top = new StackNode( object, top );
++size;
}
template<class T>
T Stack<T>::pop() {
StackNode * topOfStack = top;
top = top->next;
T data = topOfStack->data;
delete topOfStack;
--size;
return data;
}