[prog] C++ strange linking behaviour

Miriam Ruiz little_miry at yahoo.es
Mon Nov 8 18:57:25 EST 2004


 --- Wolfgang Petzold <petzold at villa-chaos.de>
escribió: 
> Hi!
> 
> I have been trying to subclass an STL container in
> order to
> add a method that can print its members nicely. So I
> entered
> the three files below and compiled them with
> 
>  g++ -Wall -g -c -o main.o main.cxx
>  g++ -Wall -g -c -o someclass.o someclass.cxx
> 
> No errors or warnings this far.
> 
> Trying to link them,
> 
>  g++ -o main main.o someclass.o
> 
> I have an error saying,
> 
>  undefined reference to
> `PrintableSet<int>::PrintableSet(void)'
> 
> The really strange thing about it is, if I copy the
> main() function
> into the someclass.cxx file, compilation and linking
> work silently
> and the resulting program prints "is: some string
> representation"
> as expected.
> 
> The same thing holds for gcc-2.95.4 with linux
> (shown above),
> gcc-3.3.1/mingw that comes with the dev-c++ IDE for
> W*n as well as
> gcc-3.3.3/cygwin; although both the gcc-3 variants
> don't miss
> PrintableSet(void) but PrintableSet(). So it isn't
> really compiler
> dependent, is it?
> 
> Any ideas what I've been doing wrong here?
> 
> Thank you for your time,
> regards
> Wolfgang

I think it has to do with the way templates work.
Templates are not compiled the same way the rest of
the code is. Instead of that, every time a templated
class is used, the code for that class is generated.

the code in someclass.cxx should be in the header file
someclass.h, because it's when the functions defined
there for a certain class are used that the code is
effectively generated.

Miry


		
______________________________________________
Renovamos el Correo Yahoo!: ¡100 MB GRATIS!
Nuevos servicios, más seguridad
http://correo.yahoo.es


More information about the Programming mailing list