Archived

This topic is now archived and is closed to further replies.

Koobazaur

Templates and includes?

Recommended Posts

I'm having a problem making and include file with templates. When I have a function with template in my main file:
template
int Triangulate(T * const Poly, fTRIANGLE * triangles, int corners)
  
it works ok, but if I put it in a separate file and include it:
fTRIANGLE *triangles = new fTRIANGLE[8];
POINT *Fig = new POINT[10];

Triangulate(Fig, triangles, 10);
  
Error: Error: Unresolved external 'Triangulate(const POINT*,fTRIANGLE*,int)' referenced from module main.cpp [edited by - Koobazaur on May 17, 2004 10:38:16 PM]

Share this post


Link to post
Share on other sites
Unless your compiler supports the export keyword, and I'm willing to bet that yours doesn't, then you can't put the definition of a template in a separate source file. The complete definition of the template needs to be available at point of instantiation, which means, in effect, that the definition needs to go into the header. (Or an inline file of some sort, etc.)

For more details see these articles: "Export" Restrictions, Part 1 and "Export" Restrictions, Part 2.

edit: and since petewood no longer posts on the boards, I suppose I should post this link for him. It's an article entitled "Instantiator: A (Mostly) Portable Framework for Separate Compilation of Templates"

edit: spelling

[edited by - SiCrane on May 17, 2004 10:53:00 PM]

Share this post


Link to post
Share on other sites
It is generally complicated and quite probably not worth your time. Unless you have some specific and convincing reason to compile your templated classes separately, don''t.

Share this post


Link to post
Share on other sites
quote:
Original post by Koobazaur
So am I understanding correctly that, unless I use some "otheR" methods, it is impossible to use template in an include file?


Other way around. It''s difficult and (very) annoying to use a template that doesn''t have its complete definition in a include file. Also, certain kinds of circular dependencies may be impossible to resolve with explicit template instantiations in separate source files.

Share this post


Link to post
Share on other sites
C++ (And C) don''t look up code from other modules during compilation. This only happens during linking. And linking can only resolve addresses, not code. This is a limitation of the language I''m afraid. Some compilers get around it with non-standard extensions. You will probably have the same problem with inline on some compilers.

Share this post


Link to post
Share on other sites
Actually that''s not quite correct. Proper implementation of the export keyword requires look-up of code from other translation units. Of course, in practice, C++ compilers don''t do this, because most compilers don''t support the export keyword. But it''s inaccurate to call it a limitation of the language when the language actually mandates it.

Share this post


Link to post
Share on other sites