I hope I'm missing something really simple here. Can anyone see why this won't compile?
I'm trying to create a dynamic queue of "event"s:
class event {
public:
int event_type;
long time;
event* next_event;
};
but the compiler stumbles when I try to define a function that will take a pointer to an "event" as an argument:
void queue_event(event *new_event_) {
// code to insert a new event into the queue will come here
};
The compiler doesn't seem to recognize the function definition, claiming:
14: error: variable or field 'queue_event' declared void
14: error: 'event' was not declared in this scope
14: error: 'new_event_' was not declared in this scope
Same result exactly if I use a reference instead of a pointer (event& new_event_). If I change the argument to be a pointer to any basic type (e.g. int* new_event_) it compiles without complaint.
Understanding compilation internals is way above my capabilities (what's preprocessing? XD) and I spent way more time than I'll ever admit staring at that snippet of code.
Preprocessing is an overloaded word and in the context I used refers to the IDE inserting the standard '#include <Arduino.h>' into your '.ino' file if you hadn't. Adding function declarations you didn't following the last include file in the '.ino' that often interfere with your usage as in your case.
There was nothing wrong with your code (that I could see). However, you have not explicitly included the declarations that would normally be required in code like that. In this case, due to the order in which you've defined your classes and functions, you didn't need to declare them in advance. However, the IDE has helpfully added declarations for you anyway. And it has screwed them up; it is the declaration for do_something_with_foo() helpfully added by the Arduino IDE which is causing the errors you encountered.
I don't understand the thinking behind the Arduino pre-processor at all. It's trying to make C++ behave a bit like Java. Which seems both pointless, and introducing needless complexity, as well as creating problems like this.
Thanks guys. This forum was my last option before candles and chanting, but with your insight I now understand that it wasn't evil spirits after all. I didn't really mean to bother you to explain preprocessing, but thanks for demystifying the issues.
class event;
void queue_event(event *new_event_);
class event {
public:
int event_type;
long time;
event* next_event;
};
void queue_event(event *new_event_) {
// code to insert a new event into the queue will come here
};
void setup ()
{
} // end of setup
void loop ()
{
} // end of loop
However without the extra two lines it doesn't compile:
class event {
public:
int event_type;
long time;
event* next_event;
};
void queue_event(event *new_event_) {
// code to insert a new event into the queue will come here
};
void setup () { }
void loop () { }
sketch_feb02b:2: error: variable or field 'queue_event' declared void
sketch_feb02b:2: error: 'event' was not declared in this scope
sketch_feb02b:2: error: 'new_event_' was not declared in this scope