Wednesday, 28 June 2017

c++ - Defining a template in .h file, vs declaring in .h file and defining in .cpp file

I have code that looks something like this (compiling using the Arduino IDE).



main sketch file:



#include "myHeader.h"


//setup stuff here, including setting up Serial & SerialBT

loop(){
CheckForMessages(&Serial); //Check USB first
CheckForMessages(&SerialBT); //Then check Bluetooth
}


myHeader.h:




#include "Arduino.h"
//other includes

template void CheckForMessages(T *port);

//declarations of other stuff


myHeader.cpp:




#include "myHeader.h"

template void CheckForMessages(T *port){
if(port->available() !=0){
//rest of function...
}

//definitions of other stuff



When I compile, I get the "undefined reference" errors for both calls to CheckForMessages in the main sketch file. This goes away if I move the definition of the template into the .h file.



I have no issues splitting the declaration and definition of functions in this way as far as I can tell, can anyone explain to me why it isn't working for my templates?

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...