Offline
Sr. Member
Karma: 6
Posts: 400
|
 |
« on: January 05, 2013, 08:43:31 pm » |
Why can I do this in a header but not in a regular .ino file? void doState1() {//..some code same with 2 and 3}
typedef void (*S_Function)();
S_Function State_Functions[] = { &doState1, &doState2, &doState3};
S_Function getCallback(int _state) { return State_Functions[_state]; }
I get S_Function does not name a type. If I put it in a header it works fine.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 120
Posts: 10194
|
 |
« Reply #1 on: January 05, 2013, 09:16:30 pm » |
The IDE extracts function prototypes and inserts them at the top of the file then feeds the result to the compiler. Essentially, this is what makes it to the compiler... S_Function getCallback(int _state);
void doState1() {//..some code same with 2 and 3}
typedef void (*S_Function)();
S_Function State_Functions[] = { &doState1, &doState2, &doState3};
S_Function getCallback(int _state) { return State_Functions[_state]; }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 6
Posts: 400
|
 |
« Reply #2 on: January 06, 2013, 12:19:31 am » |
What would be the syntax to make that function return the same thing without the typedef? Is there a way to declare that so that it works?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 6
Posts: 400
|
 |
« Reply #3 on: January 06, 2013, 12:25:35 am » |
I found it. void (*getCallback(int))();
Can I do that for a prototype and then use my typedef when I define the function later? EDIT: Why no I sure can't!
|
|
|
|
« Last Edit: January 06, 2013, 12:33:16 am by Delta_G »
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 6
Posts: 400
|
 |
« Reply #4 on: January 06, 2013, 12:35:20 am » |
This compiles. void doState1(){} void doState2(){} void doState3(){}
typedef void (*S_Function)();
S_Function State_Functions[] = { &doState1, &doState2, &doState3};
void (*getCallback(int _state))() { return State_Functions[_state]; }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 120
Posts: 10194
|
 |
« Reply #5 on: January 06, 2013, 02:01:42 am » |
Is there a reason you don't want to put the typedef in a header file?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 9
Posts: 251
|
 |
« Reply #6 on: January 06, 2013, 04:35:18 am » |
void doState1() {//..some code same with 2 and 3}
You've commented out the end brace here, // comments out all the text until the next newline, switch it to: void doState1() { //..some code same with 2 and 3 }
The reason it probably works in the header is that you haven't included your header, so it doesn't try and compile it
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 6
Posts: 400
|
 |
« Reply #7 on: January 06, 2013, 11:13:46 am » |
Is there a reason you don't want to put the typedef in a header file?
Not really. I just had a really short piece of code that used something similar and it bugged me that it didn't work all in one file. Is the fix because the includes get compiled before the sketch? Or is it because the IDE only creates prototypes for functions in the .ino but not in the headers? Which problem is it solving? Is it getting the typedef earlier or the prototype later? Does this mean that every function prototype I have in an .ino file is worthless and always get's superseded by one created by the IDE? Does this mean I can just go willy-nilly writing functions all over an .ino file anywhere I please and expect them to work anywhere I call them?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #8 on: January 06, 2013, 11:15:13 am » |
S_Function State_Functions[] = { &doState1, &doState2, &doState3};
S_Function getCallback(int _state) { return State_Functions[_state]; } The actual code should never show up in a .h file - they belong to the .c/.cpp file.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 6
Posts: 400
|
 |
« Reply #9 on: January 06, 2013, 11:20:37 am » |
The reason it probably works in the header is that you haven't included your header, so it doesn't try and compile it
The code with the header file had the include. I've done that many times over. It's the code with everything in one file that won't ever compile. I've never figured out how to have a function return a typedef type. Now I know why.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #10 on: January 06, 2013, 11:50:47 am » |
All a header file does is to tell the compiler how code should interact with each other, and to provide some convenience to the programmer.
The code goes into the source files / libraries.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 120
Posts: 10194
|
 |
« Reply #11 on: January 06, 2013, 02:15:29 pm » |
Is the fix because the includes get compiled before the sketch? Yes. Or is it because the IDE only creates prototypes for functions in the .ino but not in the headers? Yes. ("auto prototyping" is only performed on the sketch; the .ino file) Which problem is it solving? Is it getting the typedef earlier or the prototype later? Exactly. Does this mean that every function prototype I have in an .ino file is worthless and always get's superseded by one created by the IDE? Pretty much. Does this mean I can just go willy-nilly writing functions all over an .ino file anywhere I please and expect them to work anywhere I call them? Pretty much.
|
|
|
|
|
Logged
|
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 988
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #12 on: January 06, 2013, 02:47:59 pm » |
Does this mean that every function prototype I have in an .ino file is worthless and always get's superseded by one created by the IDE?
Wrong! The IDE only creates prototypes for functions in the .ino which do not already have one. Observe: void doState1() { //..some code same with 2 and 3 } void doState2() { //..some code } void doState3() { //..some code }
typedef void (*S_Function)();
S_Function State_Functions[] = { &doState1, &doState2, &doState3};
S_Function getCallback(int _state); //create function prototype here, and the IDE wont add one at the top. S_Function getCallback(int _state) { return State_Functions[_state]; }

|
|
|
|
« Last Edit: January 06, 2013, 02:49:32 pm by Tom Carpenter »
|
Logged
|
~Tom~
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #13 on: January 06, 2013, 07:24:22 pm » |
Wrong! The IDE only creates prototypes for functions in the .ino which do not already have one.
In recent versions. You need 1.0.3 onwards for this to work (or maybe 1.0.2, I can't remember right now).
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 6
Posts: 400
|
 |
« Reply #14 on: January 06, 2013, 09:03:20 pm » |
Wrong! The IDE only creates prototypes for functions in the .ino which do not already have one.
I tried to put a prototype and it didn't help. In recent versions. You need 1.0.3 onwards for this to work (or maybe 1.0.2, I can't remember right now).
Aha. That explains things. I should update my IDE.
|
|
|
|
|
Logged
|
|
|
|
|
|