Advertisement Jump to content
Sign in to follow this  

GCC 3.4.2 - Undefined reference (templates)

This topic is 5073 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm doing some hardcore template magic for a small project I've been working on for a while. I've tried compiling it on GCC 3.4.x and I'm hitting a load of problems, the majority of which I was able to solve without much trouble, but this one is eluding me. I'm getting an underfied reference whilst linking my project. I have a (huge) template class which has the following (snipped) definition:
template <class T_NATIVE, class T_API>
class gmBind {

// Export this type
typedef gmBind<T_NATIVE, T_API>    T_OBJ;

// Type relies upon template param type
typedef bool ( *gmBindPropertyFP )( T_NATIVE *p, gmThread * a_thread, gmVariable * a_operands );

// internal structure that implictly relies upon template param type
struct gmBindPropertyFunctionPair { gmBindPropertyFP getter, gmBindPropertyFP setter };

// Declare a type for my hashmap (gmBindHashMap is external to the class)
typedef gmBindHashMap< gmBindPropertyFunctionPair >   T_FPMAP;

// Finally, a static member to hold the hashmap
static T_FPMAP m_propertyFunctions;


I'm making a class gmBomb which inherits from gmBind with class gmBomb : public gmBind<bomb, gmBomb> Now, when I come to initialise my m_propertyFunctions member I get an undefined reference. I'm using: template<> gmBomb::T_OBJ::T_FPMAP gmBomb::T_OBJ::m_propertyFunctions; I've solved all the other errors but can't pin this one down. I'm assuming that the compiler is 'fogetting' about my type as it appears unreferenced and therefore the linker doesn't know about it. Everything is fine in VisualC++, and in 3.3.x GCC versions, so I'm guessing that something changed in GCC 3.4.x. Does anyone know how to solve this issue? Give me a shout if you need any more info. Thanks,

Share this post

Link to post
Share on other sites
Original post by evolutional

gmBomb::T_OBJ::T_FPMAP gmBomb::T_OBJ::m_propertyFunctions;

Well this isn't normal, and besides you only need to define m_propertyFunctions once in where ever gmBind is declared and defined put this in the same place as where gmBind is declared (if its in a header it needs to go there) and it will work:

template < typename T_NATIVE, typename T_API >
typename gmBind< T_NATIVE, T_API >::T_FPMAP gmBind< T_NATIVE, T_API >::m_propertyFunctions;

Then is no need to to keep defining it every time you inherit from gmBind, just make sure you have inclusion guards to prevent multiple definitions.

If you need to initialize m_propertyFunctions differently for gmBomb then do a (partial) specialization:

template <>
gmBind<bomb, gmBomb>::T_FPMAP gmBind<bomb, gmBomb>::m_propertyFunctions = //something else;

[Edited by - snk_kid on March 3, 2005 5:04:36 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!