Compiler error

Hello
I've got a problem which i think is related to a scope issue. But i'm completely blank and have tried a lot of different stuff to get rid of the compiler error. Here is the code of a simple state machine:

/* statemachine */

enum {ms_idle,ms_start,ms_stop};

typedef struct{
 int state,oldstate;
 int time;
}smtype;

void sm_update(smtype *p);

smtype mission;

void setup(){
  Serial.begin(9600);
  mission.state=ms_idle;
}

void loop(){
  sm_update(&mission);
  switch(mission.state){
    case ms_idle:
    Serial.print("state idle");
    delay(5000);
    mission.state=ms_start;
    break;
        
    case ms_start:
    Serial.print("state start");
    delay(5000);
    mission.state=ms_stop;
    break;
    
    case ms_stop:
    Serial.print("state stop");
    delay(5000);
    break;
    
  }      
}

void sm_update(smtype *p){
  if (p->state!=p->oldstate){
       p->time=0;
       p->oldstate=p->state;
   }
   else {
     p->time++;
   }
}

This results in a compiler error:
statemachine:1: error: variable or field 'sm_update' declared void
statemachine:1: error: 'smtype' was not declared in this scope
statemachine:1: error: 'p' was not declared in this scope

The compiler is complaining over the void sm_update(smtype *p)
function.

Hope someone can give me a clue what's wrong in this code.

Cheers

I think you're right about the scope problem. If I add a scope resolution operator to the two function headers, the code compiles fine:

void sm_update(::smtype *p)

I don't know enough about the Arduino IDE to explain why, though. Maybe your free function gets wrapped up in some namespace?

There's something that the Arduino compiler does not like about your structure definition. I changed it to

struct st{
 int state,oldstate;
 int time;
};

Then changed all occurrences of smtype to struct st, and it compiled.

try to define the typedef in a separate .h file as the IDE autogenerates function prototypes before the definition of smtype.

Thanks for the quick replies :slight_smile: