Pages: [1]   Go Down
Author Topic: Does Arduino have a problem with pointers to classes?  (Read 712 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post something that allows us to duplicate the error.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well the ever helpful IDE preprocessing hits again ...

If you can live  with it:

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks!

Understanding compilation internals is way above my capabilities (what's preprocessing?  smiley-lol) and I spent way more time than I'll ever admit staring at that snippet of code.

and thanks again
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11197
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what's preprocessing?  smiley-lol)

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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This compiles:

Code:
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
 
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
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

Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was speaking of my post at reply #3
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
void do_something_with_foo(class foo* a_new_foo_object) {
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 225
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Pages: [1]   Go Up
Jump to: