Loading...
Pages: [1] 2   Go Down
Author Topic: Why does this work in a .h but not in a .ino?  (Read 421 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 6
Posts: 400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why can I do this in a header but not in a regular .ino file?



Code:

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 Offline
Shannon Member
*****
Karma: 120
Posts: 10194
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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...

Code:
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 Offline
Sr. Member
****
Karma: 6
Posts: 400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Sr. Member
****
Karma: 6
Posts: 400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found it.

Code:
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 Offline
Sr. Member
****
Karma: 6
Posts: 400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This compiles. 

Code:
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 Offline
Shannon Member
*****
Karma: 120
Posts: 10194
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Is there a reason you don't want to put the typedef in a header file?
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 251
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
Code:
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 Offline
Sr. Member
****
Karma: 6
Posts: 400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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 Offline
Edison Member
*
Karma: 114
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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 Offline
Sr. Member
****
Karma: 6
Posts: 400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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 Offline
Edison Member
*
Karma: 114
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Shannon Member
*****
Karma: 120
Posts: 10194
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is the fix because the includes get compiled before the sketch?

Yes.

Quote
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)

Quote
Which problem is it solving?  Is it getting the typedef earlier or the prototype later?

Exactly.

Quote
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.

Quote
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 Offline
God Member
*****
Karma: 35
Posts: 988
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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];
}

smiley
« Last Edit: January 06, 2013, 02:49:32 pm by Tom Carpenter » Logged

~Tom~

Global Moderator
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 219
Posts: 13896
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Sr. Member
****
Karma: 6
Posts: 400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Pages: [1] 2   Go Up
Print
 
Jump to: