San Pedro, CA.
Offline
Full Member
Karma: 2
Posts: 122
My head HURTS!!!
|
 |
« on: November 26, 2012, 06:53:02 am » |
I'm trying to learn templates.  I've been reading a tutorial (Web page: http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part-1 ). I copied an example from the tutorial but when I compile I get an error: 'TYPE' does not name a type, it highlights the error line as the first line, the line declaring the template. Here's my sketch: template<class TYPE> TYPE Add(TYPE n1, TYPE n2) { TYPE result; result = n1 + n2; return result; }
void setup() { float x = 2.1; float y = 5.6; Serial.begin(115200); Serial.println(Add(x, y)); }
void loop() { }
I thought the purpose of using a template (in this case) was so you didn't have to (explicitly) name a type. Can someone help me understand why I'm getting an error and the author of the tutorial (obviously) is not. Thanks for your time, DJ
|
|
|
|
« Last Edit: November 26, 2012, 08:21:18 am by DigitalJohnson »
|
Logged
|
DigitalJohnson
|
|
|
|
North Queensland, Australia
Offline
Edison Member
Karma: 30
Posts: 1167
|
 |
« Reply #1 on: November 26, 2012, 07:33:46 am » |
This is the IDE's fault, add the function prototype above the definition: template<class TYPE> TYPE Add(TYPE n1, TYPE n2); The IDE otherwise generates a prototype as: TYPE Add(TYPE n1, TYPE n2); leaving TYPE as undefined.
|
|
|
|
|
Logged
|
|
|
|
|
San Pedro, CA.
Offline
Full Member
Karma: 2
Posts: 122
My head HURTS!!!
|
 |
« Reply #2 on: November 26, 2012, 08:26:22 am » |
@pYro_65 I did figure out it was the IDE when it compiled using atmel studio. I had no idea how to get it to compile in the arduino IDE. I'll give this a try and let you know if it works for me.  Thanks, DJ
|
|
|
|
|
Logged
|
DigitalJohnson
|
|
|
|
San Pedro, CA.
Offline
Full Member
Karma: 2
Posts: 122
My head HURTS!!!
|
 |
« Reply #3 on: November 26, 2012, 08:48:52 am » |
Nope. Same error: 'TYPE' does not name a type. My sketch: template<class TYPE> TYPE Add(TYPE n1, TYPE n2);
TYPE Add(TYPE n1, TYPE n2) { TYPE result; result = n1 + n2; return result; }
void setup() { float x = 2.1; float y = 5.6; Serial.begin(115200); Serial.println(Add(x, y)); }
void loop() { }
The error is at line 3. I was mistaken in my first post. The error was in the same place as indicated this time. I'm using a Mega1280 & IDE1.0.2. Any further thoughts/ideas. As I stated above I can get this to compile using atmel studio. It's just easier to do short code tests using the arduino IDE. Thanks again for any help, DJ
|
|
|
|
|
Logged
|
DigitalJohnson
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #4 on: November 26, 2012, 09:26:04 am » |
You may need to put your template function declaration in a separate header file, and include it in the sketch.
By the time you get to templates, this should be second nature.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #5 on: November 26, 2012, 02:45:33 pm » |
template<class TYPE> TYPE Add(TYPE n1, TYPE n2);
TYPE Add(TYPE n1, TYPE n2) { TYPE result; result = n1 + n2; return result; }
Yes, but the function above does not match the prototype. This compiles under IDE 1.0.2: template<typename TYPE> TYPE Add(TYPE n1, TYPE n2);
template<typename TYPE> TYPE Add(TYPE n1, TYPE n2) { TYPE result; result = n1 + n2; return result; }
It won't compile under 1.0.1 because that doesn't honour function prototypes when the IDE tries to generate its own.
|
|
|
|
|
Logged
|
|
|
|
|
San Pedro, CA.
Offline
Full Member
Karma: 2
Posts: 122
My head HURTS!!!
|
 |
« Reply #6 on: November 28, 2012, 01:12:52 am » |
Thanks Nick, that worked. I didn't know the template was part of the function declaration. By the time you get to templates, this should be second nature. Does this mean I'm in over my head?... You're probably right.  Thanks for the replies, DJ
|
|
|
|
|
Logged
|
DigitalJohnson
|
|
|
|
Offline
God Member
Karma: 14
Posts: 998
Arduino rocks
|
 |
« Reply #7 on: November 28, 2012, 02:33:43 am » |
Templates are fun, especially when you start programming with them: template <unsigned long n> struct Factorial { static const unsigned long val = Factorial<n-1>::val * n; }; template <> struct Factorial <0> { static const unsigned long val = 1; };
void setup() { Serial.print(" 3! = "); Serial.print(Factorial<3>::val); Serial.print(" 4! = "); Serial.print(Factorial<4>::val); Serial.print(" 6! = "); Serial.print(Factorial<6>::val); Serial.print("20! = "); Serial.print(Factorial<20>::val); Serial.print("30! = "); Serial.print(Factorial<30>::val); } void loop(){}
|
|
|
|
|
Logged
|
|
|
|
|
San Pedro, CA.
Offline
Full Member
Karma: 2
Posts: 122
My head HURTS!!!
|
 |
« Reply #8 on: November 28, 2012, 04:01:46 am » |
@WizenedEE You forgot Serial.begin(); To be honest I have no idea how your example works.  I'm not that good at coding yet. But thanks for it anyway. I'll study it some more and see if I can figure it out. DJ
|
|
|
|
|
Logged
|
DigitalJohnson
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #9 on: November 28, 2012, 04:10:20 am » |
He's trying to send the compiler into a loop. 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #10 on: November 28, 2012, 06:43:38 am » |
Templates are fun, especially when you start programming with them I think that that example is a bit contrived, since factoral doesn't make sense for anything other than ints. What would the function body look like for: template <String> struct Factorial { }; template <> struct Factorial <0> { };
void setup() { Serial.print(" Joe! = "); Serial.print(Factorial<"Joe">::val); } void loop(){}
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 14
Posts: 998
Arduino rocks
|
 |
« Reply #11 on: November 30, 2012, 12:04:37 am » |
Templates are fun, especially when you start programming with them I think that that example is a bit contrived, since factoral doesn't make sense for anything other than ints. The template's parameter type is unsigned long, not class or typename. It's just a way to shift the processing from the arduino to the compiler.
|
|
|
|
|
Logged
|
|
|
|
|
|