Template Member function undfined

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xillez
    New Member
    • Jul 2013
    • 93

    Template Member function undfined

    I have a problem with undefined templated member function used in main.

    Error:
    "CMakeFiles/Framework.dir/source/src/main.cpp.o: In function `main':
    main.cpp:(.text +0x33): undefined reference to `unsigned int EntityMgr::crea teEntity<Entity >()'"

    Can someone please help me understand why the EntityMgr::crea teEntity function is undefined from main?

    Current setup (simplified):

    Code:
    //EntityMgr.hpp
    #include "Entity.hpp"
    class EntityMgr
    {
        template<typename>
        unsigned int createEntity();
    }
    
    // Entity.hpp:
    class Entity
    {
        //
    };
    EntityMgr.cpp:
    Code:
    template<typedef Class>
    unsigned int EntityMgr::createEntity()
    {
        ...
    }

    main.cpp:
    Code:
    #include "EntityMgr.hpp"
    
    int main()
    {
        EntityMgr entityMgr;
        entityMgr.createEntity<Entity>();  <--- Problem line.
        return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I fixed your code. Look how I parameterize template<> and compare to the original code. Using "typedef" or "typename" is bogus because those are not types. Using "class T" defines T as a type. That is the T used by the precompiler to generate your specialized template function.

    Also be clear on these terms: A template function is the function generated by the precompiler from a function template.

    These two terms are often mixed up and this can lead to confusion when trying to understand how things work.


    Code:
    //EntityMgr.hpp
    //#include "Entity.hpp"
    class EntityMgr
    {
    public:
    
    	template<class T>
    	unsigned int createEntity();
    };
    
    // Entity.hpp:
    class Entity
    {
    	//
    };
    
    template<class T >
    unsigned int EntityMgr::createEntity()
    {
    	return 0;  //for compile only
    }
    
    //#include "EntityMgr.hpp"
    
    int main()
    {
    	EntityMgr entityMgr;
    	entityMgr.createEntity<Entity>();  //<-- - Problem line.
    		return 0;
    }

    Comment

    Working...