473,383 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Template Member function undfined

93 64KB
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.  
Aug 1 '18 #1
1 1413
weaknessforcats
9,208 Expert Mod 8TB
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. }
Aug 2 '18 #2

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

Similar topics

5
by: Bruce Lee Roy | last post by:
Hi, I am using visual studio.net and I am having problems using this bit of code when I create an instance, //////////////////////////////////////////// template<class T> class matrix : public...
3
by: Jason Heyes | last post by:
How can I define the member function of this template class using VC++ 6.0? The only way I got it to work was to define the member function inline. template <typename T> class Seq { public:...
9
by: Ian | last post by:
Can it be done? If so, what's the syntax. For example a full specialisation, template <typename T> struct X { template <typename C> void foo( C a ) {} };
4
by: pascal.cathier | last post by:
Is it possible to fully specialize a template member function? If not, why is that so? The following code fails to compile with GCC: class A { public: template < typename T > void foo() {} ...
13
by: john.constantine | last post by:
Hi I have this code: template<typename ClassType> struct S { template<typename FunctionType> void member() {}; };
5
by: rf | last post by:
As I understand it, it is legal to have a template member function of a non-template class, as in the following example: class XYZ { public: template <class Tvoid fn(T* ptr) { T val = *ptr;...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
16
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
2
by: subramanian100in | last post by:
consider the following program #include <iostream> using namespace std; class Rec { public: Rec(int arg = 10) : val(arg) { }
3
by: Peng Yu | last post by:
Hi, I have the following program. The error line is commented. I want to call _a's member function 'doit' with the template argument of function<T>. Would you please what is the correct way to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.