[TriLUG] OT: A C++ Templated Class Problem

Owen Berry oberry at trilug.org
Fri Apr 14 12:54:20 EDT 2006


On Fri, Apr 14, 2006 at 12:40:07PM -0400, Randy Barlow wrote:
> Owen Berry wrote:
> >I think you need to inline all your template functions. In other words,
> >all your code needs to be in the header file.
> >
> >  
> Hmm, that did seem to resolve the linker errors, thanks!  Is there any 
> way to do it without putting all the code in the header though?  I only 
> ask because I just wanted to keep things clean and easy to read and 
> understand by separating the interface from the implementation.
> 

Well, remember that normally you would get an object file for each cpp
file you compile. Well, now you really need an object file for each time
you instantiate the template class with a different template type, so
that templated class is defined fully somewhere, and can be linked in by
the linker. Starts getting really complicated. Too complicated for the
compiler, apprently. That's a short, and hopefully, clear way of looking
at it.

Templates are powerful, but IMHO they just aren't that easy to read and
understand in the first place. If you really want clean, generate the
code for each template type. I don't know of anything off-hand to do
that for you.

Owen



More information about the TriLUG mailing list