|
-
January 8th, 2006, 03:37 PM
#1
compiling a templated class
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"
Code:
#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"
Code:
#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"
Code:
#include"Stack.h"
using namespace std;
int main()
{
Stack<int> q;
return 0;
}
EROR
Code:
------ 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 ==========
-
January 8th, 2006, 03:47 PM
#2
Re: compiling a templated class
remove "#include "stack.h"" from stack.cpp, and instead put "#include "stack.cpp"" at the bottom of stack.h.
-
January 8th, 2006, 03:56 PM
#3
Re: compiling a templated class
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"
Code:
------ 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 ==========
-
January 8th, 2006, 04:23 PM
#4
Re: compiling a templated class
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
-
January 8th, 2006, 04:39 PM
#5
Re: compiling a templated class
 Originally Posted by sublyme
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.
-
January 8th, 2006, 05:44 PM
#6
Re: compiling a templated class
thanks for the help guys i really appreciate it. everything works fine now i did what you said googler.
-
January 8th, 2006, 06:10 PM
#7
Re: compiling a templated class
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|