Message Board
Message Board > C/C++ > Undefined reference when using templates |
March 28, 2008, 12:35 | |
DTM
Earthling! 823 posts |
I posted this elsewhere but didn't get any replies ... So I came crawling back here I get this linker error: Code: C:\blahblah\Game Framework\source\Game.o(.text+0x316) In function `ZN3irr14IEventReceiverD1Ev': [Linker error] undefined reference to `void LComponentFactory::RegisterNewComponent<LComponentCharacterDynamics2>(irr::core::string<wchar_t, irr::core::irrAllocator<wchar_t> >)' The particular code line which causes the problem: Code: componentFactory->RegisterNewComponent <LComponentCharacterDynamics2>(L"character_dynamics2"); I'm new to templates, and may have missed something... I've been sort of following this idea here: http://www.gamedev.net/referen … article2097.asp So I can allow for custom components, which are created by the factory. So I can just derive a class from my LComponent base class, and call myFactory->RegisterNewComponent<ComponentClass>("unique_name"); Some other stuff in case it helps... the component factory contains this map (private), containing function pointers to create each component (from the name). Code: core::map<core::stringw, LComponent *(*)(LPointers *, LMessageManager *, E_ID, XMLNode &)> componentCreators; Function to create a component (not belonging to any class)... Code: template<typename ComponentClass> LComponent *TCreateComponent(LPointers *pointers, LMessageManager *messageManager, E_ID eID, XMLNode &xmlNode) { return new ComponentClass(pointers, messageManager, eID, xmlNode); } and component registering (method of the component factory). Adds a pointer to the component creation function Code: template <class ComponentClass> void LComponentFactory::RegisterNewComponent(core::stringw type) { componentCreators.insert(type, &TCreateComponent<ComponentClass>); } The strange thing is, registering of the built in components works fine. These are registered in the factory constructor. But it is only registering a Component elsewhere in the program that RegisterNewComponent gives linker errors. Any ideas? ____________ :o |
# |
March 28, 2008, 20:21 | |
Htbaa
Perl 368 posts |
Templates can give headaches when used inside a class. I wanted to template a method because I wanted to be able to return different datatypes. In the end I gave up because custom datatypes and native datatypes (integers, floats etc.) were no cooperation. But what is it that you want to do with your Factory? ____________ blog.htbaa.com |
# |
March 29, 2008, 00:04 | |
DTM
Earthling! 823 posts |
Ha! You inspired me to google "c++ templates inside a class"... and I found this: http://www.comeaucomputing.com … s/#whylinkerror Inlining all the templated functions in the header file has made it work :-) And if I'd googled "Undefined reference when using templates" I'd have found the answer too. Hmm [Edited on March 29, 2008 by DTM] ____________ :o |
# |
Message Board > C/C++ > Undefined reference when using templates