Go Down

Topic: Support for templates (Read 76 times) previous topic - next topic

Blueglide

May 18, 2015, 04:50 pm Last Edit: May 18, 2015, 05:06 pm by Blueglide
Does the Arduino compiler support templates? (I'm using ver 1.0.3)

I get the following error message when compiling the attached code ???
"UsingMax:4: error: 'T' does not name a type"


Thanks for any comment.

Code: [Select]
#include <Arduino.h>

template<class T>
T kmax(T x, T y) {
   return(x > y) ? x : y;
}   

void setup() {
   Serial.begin(9600);
   Serial.println(kmax(25,9));
}
void loop() {}

pYro_65

#1
May 18, 2015, 05:28 pm Last Edit: May 18, 2015, 05:29 pm by pYro_65
Its a feature of the IDE pre processing.

Try this:
Code: [Select]
#include <Arduino.h>

template<class T>
T kmax(T x, T y) throw(){
   return(x > y) ? x : y;
}   

void setup() {
   Serial.begin(9600);
   Serial.println(kmax(25,9));
}
void loop() {}


Long story short, the throw() tricks the IDE and it doesn't produce a prototype. It incorrectly prototypes templates. My throw() trick or adding a prototype manually will fix the issue.


I have written some articles on this if you're interested:
http://arduino.land/FAQ/content/2/13/en/my-class_reference-won_t-work-in-the-sketch.html
http://arduino.land/FAQ/content/1/3/en/what-does-the-ide-change-in-my-sketch.html

Blueglide

Thanks pYro_65.  That worked! Very helpful.  

I'll spend more time with the articles.  No telling what else I will learn!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy