error LNK2020: unresolved token

T

tllee

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 stack
T* 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<< endl;

cout << "Popping elements from fs:" << endl;
while (fs.pop(f))
{
cout << f << ' ';
}
cout << endl << "Stack empty." << endl<< endl;
}
//--------------------------------end----------------------------------------------------------------------------
 
D

Doug Harrison [MVP]

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:

<snip>

Your template implementation code needs to go into the header with the
class template; it has to be available wherever the compiler needs to
instantiate the template.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top