Actually, with templates, the template definitions have to be available to 
the source at compile time... you can't just link it in.  Soon (I hope???) 
the "export" keyword will be supported, and then you should be able to use 
"good practice" by putting the function definitions in a .cpp file instead 
of a .h file (though I believe that will just be a compiler trick to go 
search for the definition and inline it anyway).

  Brian

On Thu, 3 Jul 2003, MIKE MacMartin wrote:

> > To instantiate a template, the compiler has to have the source available.
> > You hid the definitions of the constructor and destructor from it by
> > putting it in b.cpp, so while it got the class definition when you included
> > the .h, it did not have the function definitions. You got the error at the
> > link stage for the same reason you would with a normal function; when the
> > compiler sees a function declaration without a definition, it assumes the
> > function will be linked later.
> 
> The problem is not that it's not being defined (it should be linked in by ld 
> if he's got his makefile set up).  You should never need to put function 
> bodies in your .h files (in fact, it's usually not recommended because it's 
> harder to get deps correct in your Makefile, more things need to be 
> recompiled etc.)
> 
> *thinks for a bit* ... wait, no, in this specific case, you're right.  I found 
> that g++ could not compile templates properly if the source was not inlined 
> in the .h file ... that is, it goes into an infinite compile loop.  They may 
> have fixed that by now, but I don't know.
> 
> > To fix this remove the #include from b.cpp and either move its contents
> > into b.h or #include b.cpp in b.h. Obviously those are the same as far as
> > the compiler is concerned. I would suggest you consult your favorite C++
> > text if you need more information; you should have a starting point now.
> 
> What he really wants to do is ensure that they're linked together.  The way 
> that's done is g++ -c b.cpp; g++ -o main main.cpp b.o;
> 
> It could likely be done in one line, but this way shows you the basis for the 
> Makefile (which you should write for any C++ project).
> 
> Also, the (I believe) syntax error in the b.cpp file, which I pointed out in 
> another message.
> 
> > -Heschi
> MIKE
> 


--
[EMAIL PROTECTED] mailing list

Reply via email to