Re: Header file + implementation file with template

From: B. v Ingen Schenau (bart_at_ingen.ddns.info)
Date: 04/30/04


Date: Fri, 30 Apr 2004 11:31:17 +0200

cpp wrote:

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

Until a sufficiently large number of compilers start supporting the export
keyword, the answer is Yes.

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

For templates that is common practice.
But keep in mind that the extension .inl is not magical. People just as
often use .tcc or even .cpp.
The idea behind this is that you still have the logical separation of
interface and implementation, even if the compiler does not support a
physical separation.

>
> Thanks.
>
Bart v Ingen Schenau

-- 
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/


Relevant Pages

  • Re: HELP! --using templates and functions
    ... in my header file I have defined: ... template ... > struct CTPoint ... > struct CTRect ...
    (microsoft.public.vc.mfc)
  • Problems with "Export Template"
    ... I am trying to set up a template .cpp and .h file for generating new classes ... I've tried different project types -- again, no success. ... All of the help implies ...
    (microsoft.public.vstudio.general)
  • Re: Why "already defined in"?
    ... Why should template functions be in the header file? ... Template and inline functions must be defined in header files because ... their definition must be visible to the compiler in every translation unit ... one-definition rule still applies. ...
    (microsoft.public.vc.language)
  • Re: header file problem
    ... i did at first time the same procedure you descirbed ... and then i try to learn using header file and definiton file ... resolving excercise it my lovely book - thniking in cpp. ... now im sure that was a compiler bug, because i create my car.h and car.cpp ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Multiple definitions
    ... >> I have a problem with multiple definitions that I can't quite straighten ... >convert is not a template function so if you want to put it in a header file ... Comeau C/C++ with Dinkumware's Libraries... ...
    (comp.lang.cpp)