I wrote a simple stack program using class template on VC++ 2003 but am unable to resolve the following link errors:
LINK : error LNK2020: unresolved token (0A000046) Stack.__dtor
LINK : error LNK2020: unresolved token (0A000048) Stack.__dtor
LINK : fatal error LNK1120: 2 unresolved externals
The following are my source codes:
//-----------------------stack.h----------------------------------------------------
template <class T>
class
Stack{
public
:Stack(
int = 10);~Stack();
bool push(const T&); bool pop(T&); bool isEmpty() const; bool isFull() const;private
: int size; // Number of elements on stack int top; // Element at top of stackT* stackPtr;
};
//------------------------stack.cpp---------------------------------------------------------
#include
"stdafx.h"#using
<mscorlib.dll>using
namespace System;#include
"Stack.h"// --------------------Stack-----------------------------
// Constructor with default size 10
// Pre initialize stack with size s or default size 10
// Post nothing
//-------------------------------------------------------
template
<class T>Stack<T>::Stack(
int s){
size = (s > 0 && s < 1000 s : 10);
top = -1;
stackPtr =
new T[size];}
// --------------------push-----------------------------
// Push a new element onto the stack
// Pre check whether stack is full
// Post return true if item pushed into stack,
// false otherwise.
//-------------------------------------------------------
template
<class T>bool
Stack<T>::push(const T& pushItem){
if (!isFull()){
stackPtr[++top] = pushItem;
return true;}
else return false;}
// --------------------pop---------------------------------
// Pop an element from the stack
// Pre check whether stack is empty
// Post return true if item popped from stack,
// false otherwise.
//---------------------------------------------------------
template
<class T>bool
Stack<T>::pop(T& popItem){
if (!isEmpty()){
popItem = stackPtr[top--];
return true;}
else return false;}
// --------------------isEmpty-----------------------------
// Check whether stack is empty
// Pre nothing
// Post return true if stack is empty,
// false otherwise.
//---------------------------------------------------------
template
<class T>bool
Stack<T>::isEmpty() const{
return (top <= -1);}
// --------------------isFull-----------------------------
// Check whether stack is full
// Pre nothing
// Post return true if stack is full,
// false otherwise.
//---------------------------------------------------------
template
<class T>bool
Stack<T>::isFull() const{
return (top >= (size - 1));}
// --------------------~Stack------------------------------
// Destroys the stack by deleting its array pointer
// Pre nothing
// Post nothing
//---------------------------------------------------------
template
<class T>Stack<T>::~Stack()
{
delete[] stackPtr;}
//-----------------------main.cpp-------------------------------#include
"stdafx.h"#include
<iostream>#include
"stack.h"#using
<mscorlib.dll>using
namespace System;using
namespace std;
void
main(){
typedef Stack<double> doubleStack;
doubleStack fs(5);
double f = 1.2;
cout << "Pushing elements onto fs:" << endl;
while (fs.push(f))
{
cout << f << ' ';
f += 1.1;
}
cout << endl << "Stack full." << endl;
cout << endl;
cout << "Popping elements from fs:" << endl;
while (fs.pop(f))
{
cout << f << ' ';
}
cout << endl << "Stack empty." << endl;
cout << endl;
}

error LNK2020: unresolved token
mike1942f
Harish
Hi, Chriz:
Template function definition needs to be seen at the point where it is instantiated. The compiler compiles one .cpp file at a time. When the compiler tries to compile the stack.cpp file, it sees the template function definition. However, there is no instantiation. As the result, the compiler does not have a concrete type so to know how to generate code for these functions.
When the compiler tries to compile the main.cpp file, it only sees the header file. So it generates reference to these functions. But since there is no code generated for these functions, you would get the link error during the link phase.
To resolve this problem, the template function definition is usually in the same file as the declaration. To use these templates, you would include the file that has both the definition and declaration. So the compiler knows how to generate the code correctly. For example, if you take a look at the <iostream> file, you will see both the template class declaration and definition are there.
In your sample problem, if you include <stack.cpp> in main.cpp, the program will build successfully. But I would suggest you to put all the code in the stack.cpp into the <stack.h> and includes the stack.h in the main.cpp.
Hope that helps.
James