Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic calling template <typename T>from another class (Read 2545 times)
drony jydnas
Newbies
*
Offline


Posts: 3
Joined: Apr 29th, 2017
calling template <typename T>from another class
Jan 15th, 2018 at 5:49pm
Print Post  
i got this exception i dont know why?

"Sketch2.ino:8: undefined reference to void ManagerClass  doSomething<char const*>(char const*)"

complete project demo attached. thank you
  

Please Register or Login to the Forum to see File Attachments
Back to top
 
IP Logged
 
drony jydnas
Newbies
*
Offline


Posts: 3
Joined: Apr 29th, 2017
Re: calling template <typename T>from another class
Reply #1 - Jan 15th, 2018 at 5:51pm
Print Post  
Code (C++)
Select All
// Manager.h

#ifndef _MANAGER_h
#define _MANAGER_h

	#include "Arduino.h"




class ManagerClass
{
 public:
	template<typename T> void doSomething(T param);
};


extern ManagerClass Manager;

#endif




//manager.cpp
include "Manager.h"

template <typename T>
void ManagerClass::doSomething(T param)
{
	Serial.println(param);
}

ManagerClass Manager;







//sketch2.ino
#include "Manager.h"

void setup() {
	Serial.begin(115200);
}

// the loop function runs over and over again until power down or reset
void loop() {

	Manager.doSomething("123123");
}


 

« Last Edit: Jan 15th, 2018 at 6:56pm by Tim@Visual Micro »  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: calling template <typename T>from another class
Reply #2 - Jan 15th, 2018 at 6:58pm
Print Post  
Put user defined types and templates in (.h and .cpp if needed) then #include in your main code.

The issue is that auto generated c++ prototypes are inserted in the code before your types are defined. You can override in most instances but it's simpler just to put the c++ stuff in external files.
« Last Edit: Jan 15th, 2018 at 6:58pm by Tim@Visual Micro »  
Back to top
WWW  
IP Logged
 
drony jydnas
Newbies
*
Offline


Posts: 3
Joined: Apr 29th, 2017
Re: calling template <typename T>from another class
Reply #3 - Jan 15th, 2018 at 9:13pm
Print Post  
I do not know much about c++. Could you give a very short example? thank you
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: calling template <typename T>from another class
Reply #4 - Jan 28th, 2018 at 4:58am
Print Post  
Hi,

You can create a .h header file and/or .cpp file in the project using the menus.

Then you add the following to the top of the .ino sketch code:-

Code (C++)
Select All
#include "my_header_file_name.h"  



If you need to put code in the .cpp then also add the #include to the top of the cpp code.

Do not give the .h or .cpp file the same name as the project/.ino

Breaking your code down into .h and .cpp files is worth exploring. There will be many examples on the google and arduino forum.
  
Back to top
WWW  
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint