[LC++]Template functions and linking

Mark Phillips mark at austrics.com.au
Mon Jul 8 16:10:06 UTC 2002


Suppose you have some template function, declared in some header
file.

Eg, dosomething.h:

   template<typename A>
   int doit(A const& a);

Then you implement this function in another file, dosomething.cc:

   template<typename A>
   int doit(A const& a) {
     int it;
     // ... do something
     return it;
   }

Finally, you want to use this function in file main.cc:

   #include "dosomething.h"

   ...
   double x;
   cout<<doit(x)<<endl;
   ...

The problem is, that when g++ creates the object file dosomething.o,
it doesn't know what typenames it should use for creating versions
of doit.  When compiling main.cc, it wants a doit<double>, but
at linking, no such beast exists.

One solution would be to put the implementation of doit in the header
and make it inline.

Is there a better way?

Thanks,

Mark.

--
Dr Mark H Phillips
Research Analyst (Mathematician)

AUSTRICS - smarter scheduling solutions - www.austrics.com

Level 2, 50 Pirie Street, Adelaide SA 5000, Australia
Phone +61 8 8226 9850
Fax   +61 8 8231 4821
Email mark at austrics.com.au





More information about the tuxCPProgramming mailing list