Mail Archives: djgpp/1997/10/21/11:32:45
Charles Krug <charles AT pentek DOT com> schrieb im Beitrag
<343D29AB DOT 6F89BB31 AT pentek DOT com>...
> I've a template class that compiles but won't link.
>
> ...
Hi Charles!
If you declare a template class in a header file
(e.g. template.h)
and separate the function definitions using an implementation
file (e.g. template.cc), the compiler won't automatically
create template class instantiations for all required types.
Now, there are two possibilities:
i) Put the declarations and definitions together in one file.
This is not the most elegant method, it has the disadvantage,
that you always need to include the complete source of the
template class, where it is required. This is likely to increase
the size of your executable, especially, when different source
files use your template class with the same type.
ii) Supposed, that 'template.h' / 'template.cc' define the
class 'Test' and you
need this class for the types 'double' and 'complex'.
Then you should create a file like this:
#include"template.cc" // template.cc should include template.h
template class Test<double>;
template class Test<complex>;
This file must be compiled and linked to the other objects
of your project.
Sincerely
Andreas B.
- Raw text -