Header file + implementation file with template

From: cpp (cpp_at_earthlink.net)
Date: 04/30/04


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()]-------



Relevant Pages

  • Re: Xmkmf/make problem
    ... > I have several Xlib programs that compiled successfully on earlier ... > get an error message: ... > It looks as though this header file, and several others, have been moved ... the best option is to fix the broken programs. ...
    (freebsd-questions)
  • Re: Newbie question: Need for header file when linking to static library
    ... advice on an error message, it is absolutely critical that you copy the error message *IN ... either you have failed to include the proper header file during the build process, ... SOMENAME void OtherFunction; ...
    (microsoft.public.vc.mfc)
  • Re: Trouble linking library
    ... LNK2001 was not an error message. ... I have no idea why people will give compiler or linker error numbers without the ... I suspect that your header file is missing the following declaration: ... But without any meaningful bug report, ...
    (microsoft.public.vc.mfc)
  • Re: GetLocalPathName could not be located in Kernel32.dll
    ... >following error message: The procedure entry point ... If you do not have this header file, ... >obtained by downloading the most recent SDK from the SDK Update Site." ... >But now I am getting the following error during compilation: ...
    (microsoft.public.dotnet.languages.vc)
  • C++ programming question
    ... The examples in the book use the header file iostream.h (#include ... When I compile the programs, I get an error message ... and where are the "modern" header files that support these functions? ...
    (alt.os.linux)