Go Down

Topic: Does Arduino have a problem with pointers to classes? (Read 1 time) previous topic - next topic

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:

Code: [Select]
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:
Code: [Select]
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

lloyddean

Post something that allows us to duplicate the error.

It's not much, but it compiles with the error:
Code: [Select]
class foo {
  // whatever
};

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

lloyddean

Well the ever helpful IDE preprocessing hits again ...

If you can live  with it:

Code: [Select]

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.

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

lloyddean

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 ...

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

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.

Nick Gammon

This compiles:

Code: [Select]

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
 
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

lloyddean

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

Nick Gammon

However without the extra two lines it doesn't compile:

Code: [Select]

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 () { }


Code: [Select]

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

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics


Nick Gammon

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

Code: [Select]

void do_something_with_foo(class foo* a_new_foo_object) {
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Professor Chaos

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


Go Up