Does Arduino have a problem with pointers to classes?

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.

Thanks in advance for any help.

Joe

1 Like

Post something that allows us to duplicate the error.

It's not much, but it compiles with the error:

class foo {
  // whatever
};

void do_something_with_foo(foo *a_new_foo_object) {
  // whatever
}

Well the ever helpful IDE preprocessing hits again ...

If you can live with it:

class foo {
  // whatever
};

void do_something_with_foo(class foo* a_new_foo_object) {
  // whatever
}

Or move the class declaration/definitions to .h/.cpp file pairs.

1 Like

Thanks!

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.

and thanks again

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.

A few other things ...

joe_weisman:
what's preprocessing? XD)

The Arduino IDE is your problem.

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.

This compiles:

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

Nick, that's really all my version does - it's a type of forward declaration.

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

I was speaking of my post at reply #3

Oh I see. I totally missed the word "class" in this line:

void do_something_with_foo(class foo* a_new_foo_object) {

One could also put the class definition in a .h file, and #include the .h file in your main sketch.