Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

template definition MUST go in the header file???

Status
Not open for further replies.

jmborr

Instructor
Feb 16, 2004
7
US
Hi all,

I have a "template.h" file with a class template declaration, and I have "template.cpp" file with the definition of this class template.

It turns out that I have the following compilation error:
undefined reference to `vec<double>::foo()'
After googling, I found a post assuring that the class template definition MUST go along in the header file. They cannot reside in two separate files

Is this for real??

Below are the two files:
//===============template.h=================
template<class T>
class vec{

public:
vec( ){ dim = 0; }
void foo(void);
protected:
int dim ;
};
//===============template.cpp================
#include<iostream.h>
using namespace std;
#include &quot;template.h&quot;

template<class T>
void vec<T>::foo(void){
cout <<&quot;foo function\n&quot;;
}
//===========================================
 
If this is the case, then I'm in trouble because I generate multiple definitions of &quot;foo&quot; !

If I include &quot;template.h&quot; into files &quot;one.cpp&quot; and &quot;two.cpp&quot;, and then I do &quot;g++ one.o two.o&quot;, I will generate two definitions of &quot;foo&quot; function.

Any standar get-around this??

jose,
 
I don't see the problem.

It worked fine for me.

Try it and post the errors you get.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top