sublyme
January 8th, 2006, 02:37 PM
what is the correct way to compile a class that uses templates. i keep getting an error msg on the linking step. can some one help?
"Stack.h"
#pragmaonce
template <class xtype>
struct node
{
xtype info;
node * next;
};
template<class xtype>
class Stack
{
private:
node<xtype> * top;
public:
Stack(void);
~Stack(void);
void Push(xtype);
void Pop(xtype&);
bool Isfull(void);
bool Isempty(void);
};
"Stack.cpp"
#include"Stack.h"
template <class xtype>
Stack<xtype>::Stack(void)
{
top = NULL;
}
template <class xtype>
Stack<xtype>::~Stack(void)
{
node<xtype>* q;
while(top != NULL)
{
q = top;
top = top->next;
delete q;
}
}
template <class xtype>
void Stack<xtype>::Push(xtype item)
{
node<xtype>* p;
p = new node<xtype>;
p->next = top;
p->info = item;
top = p;
}
template <class xtype>
void Stack<xtype>::Pop(xtype& item)
{
if( !Isempty() )
{
item = top->info;
node<xtype>* p;
p = top;
top = top->next;
delete p;
}
}
template <class xtype>
bool Stack<xtype>::Isfull()
{
node<xtype>* freestore;
freestore = new node<xtype>;
if(freestore == NULL)
return true;
else
{
delete freestore;
return false;
}
}
template <class xtype>
bool Stack<xtype>::Isempty()
{
return (top == NULL);
}
"main.cpp"
#include"Stack.h"
using namespace std;
int main()
{
Stack<int> q;
return 0;
}
EROR
------ Build started: Project: LinkedStack, Configuration: Debug Win32 ------
Compiling...
main.cpp
Stack.cpp
Generating Code...
Linking...
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Stack<int>::~Stack<int>(void)" (??1?$Stack@H@@QAE@XZ) referenced in function _main
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Stack<int>::Stack<int>(void)" (??0?$Stack@H@@QAE@XZ) referenced in function _main
C:\CPLUSPLUS\LinkedStack\Debug\LinkedStack.exe : fatal error LNK1120: 2 unresolved externals
Build log was saved at "file://c:\CPLUSPLUS\LinkedStack\Debug\BuildLog.htm"
LinkedStack - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
"Stack.h"
#pragmaonce
template <class xtype>
struct node
{
xtype info;
node * next;
};
template<class xtype>
class Stack
{
private:
node<xtype> * top;
public:
Stack(void);
~Stack(void);
void Push(xtype);
void Pop(xtype&);
bool Isfull(void);
bool Isempty(void);
};
"Stack.cpp"
#include"Stack.h"
template <class xtype>
Stack<xtype>::Stack(void)
{
top = NULL;
}
template <class xtype>
Stack<xtype>::~Stack(void)
{
node<xtype>* q;
while(top != NULL)
{
q = top;
top = top->next;
delete q;
}
}
template <class xtype>
void Stack<xtype>::Push(xtype item)
{
node<xtype>* p;
p = new node<xtype>;
p->next = top;
p->info = item;
top = p;
}
template <class xtype>
void Stack<xtype>::Pop(xtype& item)
{
if( !Isempty() )
{
item = top->info;
node<xtype>* p;
p = top;
top = top->next;
delete p;
}
}
template <class xtype>
bool Stack<xtype>::Isfull()
{
node<xtype>* freestore;
freestore = new node<xtype>;
if(freestore == NULL)
return true;
else
{
delete freestore;
return false;
}
}
template <class xtype>
bool Stack<xtype>::Isempty()
{
return (top == NULL);
}
"main.cpp"
#include"Stack.h"
using namespace std;
int main()
{
Stack<int> q;
return 0;
}
EROR
------ Build started: Project: LinkedStack, Configuration: Debug Win32 ------
Compiling...
main.cpp
Stack.cpp
Generating Code...
Linking...
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Stack<int>::~Stack<int>(void)" (??1?$Stack@H@@QAE@XZ) referenced in function _main
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Stack<int>::Stack<int>(void)" (??0?$Stack@H@@QAE@XZ) referenced in function _main
C:\CPLUSPLUS\LinkedStack\Debug\LinkedStack.exe : fatal error LNK1120: 2 unresolved externals
Build log was saved at "file://c:\CPLUSPLUS\LinkedStack\Debug\BuildLog.htm"
LinkedStack - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========