Header file + implementation file with template
From: cpp (cpp_at_earthlink.net)
Date: 04/30/04
- Next message: Ronen Kfir: "long veriable causes problem"
- Previous message: Uwe Schnitker: "Re: Initialize dynamic allocated data before main get error?"
- Next in thread: B. v Ingen Schenau: "Re: Header file + implementation file with template"
- Reply: B. v Ingen Schenau: "Re: Header file + implementation file with template"
- Reply: Martin Gieseking: "Re: Header file + implementation file with template"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 30 Apr 2004 07:21:41 GMT
Hi folks,
I have a problem on using header file and template.
If I put the interface/declaration of a class template in a header
file (foo.h) and put the implemenation in to a cpp file (foo.cpp).
When I compile it using gcc 3.3.2 (cygwin) and 2.5.0(redhat):
g++ -c foo.cpp (Ok here, generate foo.o)
g++ -o test test.cpp foo.o (linking error here)
Error message:
/cygdrive/d/code/ccqie7hk.o(.text+0x25):test.cpp: undefined reference
to 'Foo<int>::bar()' collect2: ld returned 1 exit status
If I move the implementation code in foo.cpp to foo.h and leave only
#include "foo.h" statement there, the linker won't complain.
My question is, do I have to put all the implementation detail in a
the header file? I searched and found
http://www.flipcode.com/cgi-bin/msg.cgi?showThread=00000905&forum=general&id=-1
Is it a common practice to #include a .inl file in a header? It seems
weird to me.
Thanks.
//---------[ foo.h ]------------------
template <typename T>
class Foo{
T data_;
public:
T bar();
};
//---------[ foo.cpp ]------------------
#include "foo.h"
template <typename T>
T Foo<T>::bar(){
return 2*data_;
}
//---------[ test.cpp ]------------------
#include "foo.h"
int main(){
Foo<int> afoo;
int result = afoo.bar();
} //-----[end of main()]-------
- Next message: Ronen Kfir: "long veriable causes problem"
- Previous message: Uwe Schnitker: "Re: Initialize dynamic allocated data before main get error?"
- Next in thread: B. v Ingen Schenau: "Re: Header file + implementation file with template"
- Reply: B. v Ingen Schenau: "Re: Header file + implementation file with template"
- Reply: Martin Gieseking: "Re: Header file + implementation file with template"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|