Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

Templating a function

Started by gellin Oct 26, 2005 at 11:05 PM 2 replies 450+ views
Original Post
gellin
gellin
Ok I'm having some trouble templating. I've only done one template function and that was over 4 years ago and am having some trouble. mainCharacter.h

template<typename T>
void checkObjectCollision( T objects );

mainCharacter.cpp

template<typename T>
void mainCharacter::checkObjectCollision( T objects )
{
}

I plan on calling checkObjectCollision(allTiles) and checkObjectCollision(allWaters) and they are defined as follows I also plan on checking for quite a few other things or else I wouldn't bother with templating

struct myStruct
{
	vector<tile> allTiles;
        vector<water> allWaters;
};

Everything compiles fine but I get this link error: error LNK2019: unresolved external symbol "public: void __thiscall mainCharacter::checkObjectCollision > >(class std::vector >)" (??$checkObjectCollision@V?$vector@Vtile@@V?$allocator@Vtile@@@std@@@std@@@mainCharacter@@QAEXV?$vector@Vtile@@V?$allocator@Vtile@@@std@@@std@@@Z) referenced in function "void __cdecl display(void)" (?display@@YAXXZ) I'm guessing it has something to do with std::allocator but don't really know. Any help would be greatly appreciated and also let me know if I'm doing something totally wrong or if there is a better way. Thanks
rmsimpson
rmsimpson
This doesn't look like the full code ... hard to determine where the fault lies without the class definition(s).

Robert
stylin
stylin
You can't split a templated function or class' declaration from its definition. This is because compilers need to see the template definition when instantiating it. You can solve this problem by putting your whole templated function - including the implementation - inside of the header file.

For a great read on templates, find C++ Templates: The Complete Guide. If you're interested in generic programming, this book is an absolute must.
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
gellin
gellin
Thanks stylin. That did the trick.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.