Templates and includes?

Started by
6 comments, last by Koobazaur 19 years, 11 months ago
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]
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
Advertisement
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]
So am I understanding correctly that, unless I use some "otheR" methods, it is impossible to use template in an include file?
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
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.
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.
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.
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.
Can you try something for me? Can you try to put .hpp as header extension and see if it works?

--
You''re Welcome,
Rick Wong
- Google | Google for GameDev.net

This topic is closed to new replies.

Advertisement