469,590 Members | 1,949 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,590 developers. It's quick & easy.

error LNK2020: unresolved token

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----------------------------------------------------------------------------

Apr 5 '06 #1
2 2779
On 4 Apr 2006 19:28:50 -0700, tl***@bloomberg.net wrote:
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.
--
Doug Harrison
Visual C++ MVP
Apr 5 '06 #2
Thanks, you're a gem!

Apr 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Tony Baker | last post: by
3 posts views Thread by Simon Jefferies | last post: by
2 posts views Thread by Vickie | last post: by
4 posts views Thread by Jun | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.