I'm an old VB4 programmer, don't know beans about C. I got a Duemilanove to control the roof on my observatory. I'm trying to declare a variable called CurrentState (before "void setup") and getting an error message:
error: expected initializer before numeric constant
code is:
#define OpenedLed 0 //Roof fully closed led green #define ClosedLed 1 //Roof fully closed led green #define OpeningLed 2 //Roof is opening led blinking red #define ClosingLed 3 //Roof is closing led blinking red #define FullyOpen 4 // Fully open switch fully open = switch closed #define FullyClosed 5 //Fully closed switch fully closed = switch closed #define CloseCommand 6 //Close roof command switch #define OpenCommand 7 //Open roof command switch #define OpenButton 8 //Open pushbutton #define CloseButton 9 //Close pushbutton
int CurrentState 0 /*1 = open, 2 = close, 3 = moving open, 4 moving close, 5 = error, 6 = startup */
If I comment out the "int CurrentState 0" it gives no error. I want CurrentState to be a global variable. Where should this variable be defined and how?
"error: variable or field 'showRoofState' declared void"
Of course this makes no sense at all unless you already know that enums don't work as you'd expect them to, or if you are very familiar with how the parser works.
To fix this you must declare the enum in a seperate .h file as described here:
Fortunately this is trivially easy to do, I just wish it were more apparent that it was necessary, I wasted most of an hour last night trying to figure it out.
Also, incidentally, in C++ it isn't necessary to include 'enum' when you use the type, you can specify it like any other type:
In a small simple program, there's not a great advantage (outside of adhering to accepted best practices).
The real advantage comes in when you start getting lots of different numeric identifiers, especially when they are spread through many different source files.
By using a named enumerator you don't have to go back and look up that 1 = open. This is particularly helpful in IDE's that have Intellisense or other code preview routines that actually shows you the choices that you have.