Arduino IDE bug?

The code below give compilation errors as follows under IDE 1.6.5:

sketch_may15a.ino:2:18: error: variable or field 'start_event' declared void
sketch_may15a.ino:2:18: error: 'EventType' was not declared in this scope

yet if I rename setup() as main() it compiles under gcc on a Raspberry Pi. So it looks like the munging the IDE does before presenting the sketch to the compiler is messing things up. Any ideas how to fix it? I presume if I simply replaced the enum by a set of integers it'd probably work, but hey, I want to write in C.

typedef enum event_type {
  type1, type2
} EventType;

struct event {
  struct event *next;
  EventType type;
  float x1;
  float x2;
  float x3;
};

void start_event(EventType type) { return; }

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

The IDE pre-processing generates function prototypes for you that look like:

void start_event(EventType type);

typedef enum event_type {
  type1, type2
} EventType;

Ie, "EventType" is used before it is defined, causing an error. Putting the definitions in a separate .h file works around this (IIRC, the generated prototypes are inserted AFTER #include statements.)

Alternatively you can declare your function as:

void start_event(enum event_type type) { return; }

That way it doesn't rely on the typedef.

Thanks all. Putting the enum declaration in a new ta, calling it event.h and then inserting
#include "event.h"
as the first line in the main tab works for reasons that are now obvious to me.

But simply removing the typedef and declaring the function as void start_even(enum event_type type) doesn't help as the enum declaration evidently isn't pulled out and included amongst the generated function prototypes.

Regards - Philip

doesn't help as the enum declaration evidently isn't pulled out and included amongst the generated function prototypes.

No, it isn't. The IDE can only generate function prototypes for functions that use only simple types. enum, struct, references, etc. are not simple types.

The best thing to do is to not expect the IDE to generate function prototypes for you. Define them yourself.

The best thing to do is to not expect the IDE to generate function prototypes for you. Define them yourself.

But that doesn't help this particular situation, because the IDE doesn't detect prototypes that already exist, and it will still put the prototypes for functions that use structures before the definitions of the structures. :frowning: