423,103 Members | 1,428 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,103 IT Pros & Developers. It's quick & easy.

Template Member function undfined

P: 84
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::createEntity<Entity>()'"

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

Current setup (simplified):

Expand|Select|Wrap|Line Numbers
  1. //EntityMgr.hpp
  2. #include "Entity.hpp"
  3. class EntityMgr
  4. {
  5.     template<typename>
  6.     unsigned int createEntity();
  7. }
  8.  
  9. // Entity.hpp:
  10. class Entity
  11. {
  12.     //
  13. };
  14.  
EntityMgr.cpp:
Expand|Select|Wrap|Line Numbers
  1. template<typedef Class>
  2. unsigned int EntityMgr::createEntity()
  3. {
  4.     ...
  5. }
  6.  

main.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include "EntityMgr.hpp"
  2.  
  3. int main()
  4. {
  5.     EntityMgr entityMgr;
  6.     entityMgr.createEntity<Entity>();  <--- Problem line.
  7.     return 0;
  8. }
  9.  
1 Week Ago #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,150
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.


Expand|Select|Wrap|Line Numbers
  1. //EntityMgr.hpp
  2. //#include "Entity.hpp"
  3. class EntityMgr
  4. {
  5. public:
  6.  
  7.     template<class T>
  8.     unsigned int createEntity();
  9. };
  10.  
  11. // Entity.hpp:
  12. class Entity
  13. {
  14.     //
  15. };
  16.  
  17. template<class T >
  18. unsigned int EntityMgr::createEntity()
  19. {
  20.     return 0;  //for compile only
  21. }
  22.  
  23. //#include "EntityMgr.hpp"
  24.  
  25. int main()
  26. {
  27.     EntityMgr entityMgr;
  28.     entityMgr.createEntity<Entity>();  //<-- - Problem line.
  29.         return 0;
  30. }
1 Week Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.