Click to See Complete Forum and Search --> : compiling a templated class


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

googler
January 8th, 2006, 02:47 PM
remove "#include "stack.h"" from stack.cpp, and instead put "#include "stack.cpp"" at the bottom of stack.h.

sublyme
January 8th, 2006, 02:56 PM
thanks googler for the help but I've tried that did it work for you. I get the infamous error:
.\Stack.cpp(3) : error C2143: syntax error : missing ';' before '<'
which I think means that my template is illegal somehow. these are all the errors i get
"ERRORS"

------ Build started: Project: LinkedStack, Configuration: Debug Win32 ------

Compiling...

Stack.cpp

.\Stack.cpp(3) : error C2143: syntax error : missing ';' before '<'

.\Stack.cpp(3) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

.\Stack.cpp(3) : error C2988: unrecognizable template declaration/definition

.\Stack.cpp(3) : error C2059: syntax error : '<'

.\Stack.cpp(8) : error C2588: '::~Stack' : illegal global destructor

.\Stack.cpp(8) : fatal error C1903: unable to recover from previous error(s); stopping compilation

main.cpp

Generating Code...

Build log was saved at "file://c:\CPLUSPLUS\LinkedStack\Debug\BuildLog.htm"

LinkedStack - 6 error(s), 0 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

ZuK
January 8th, 2006, 03:23 PM
For most compilers you have to keep template-code ( declaration and implementation ) in one file. Looks like you are using an MS-compiler. This one cannot handle the extern - keyword.
The problem is that the compiler cannot know where you have put the implementation of your template-functions if you #include the declaration-part only.
BTW you cannot compile templates. Code is generated when you instantiate a template.
K

googler
January 8th, 2006, 03:39 PM
thanks googler for the help but I've tried that did it work for you.
Oops, sorry, I forgot. You shouldn't compile Stack.cpp directly as a source file in your project. Use the project settings to mark it as "exclude from build". You should only compile main.cpp in your example.

sublyme
January 8th, 2006, 04:44 PM
thanks for the help guys i really appreciate it. everything works fine now i did what you said googler.

Graham
January 8th, 2006, 05:10 PM
Have a look at this FAQ. (http://www.codeguru.com/forum/showthread.php?t=250284)