Re: Help compiling cpp program with templates

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jan 07, 2004 at 12:37:53PM +0100,   wrote:
>   I tried to use a simple template in a class, and it
> worked when all the definition where in the same file.
> But when I separated the code in two cpp files, I get
> a linker error, and I couldn't firgure out how to
> compile it.
>  I tried the following commands:
>   g++ -c test.cpp
>   g++ -c test1.cpp
>   g++ -o test test.o test1.o
> 
> The first two worked, but i got the linker error at
> the last one.
> I attached the files to these mail.
> Thank you.

You can't seperate a template class and its constructor like this.
A template class with all its methods must be fully available at 
instantiation time (as of now -- this may change in the future when the
`export' keyword is supported).

Usually you do it like this:

// test.h

#ifndef TEST_H
#define TEST_H

template <class T> foo {
  ...
  test();
};

#include <test.tcc>

#endif // TEST_H

----------------------------------------------------------------------

// test.tcc
// (gets included from test.h)

template <class T> test<T>::test( ) {
}

----------------------------------------------------------------------

// test.cc

#include "test.h"

...

----------------------------------------------------------------------

Of course, you may choose another suffix instead of .tcc for your
template implementation files, but that's the same suffix libstdc++ uses 
for these files.

HTH
-- 
Claudio Bley                                 ASCII ribbon campaign (")
Debian GNU/Linux user                         - against HTML email  X 
http://www.cs.uni-magdeburg.de/~bley/                     & vCards / \

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux