Pointer to class

I am long time c, just starting c++, why doesn’t this compile?

I get ptr “does not name type” error.

#include "application.h"

class foo{
  public:
    int bar;
    int barr;
};

foo myfoo;
foo *ptr;
ptr = &myfoo;

void setup(){
    delay(1000);

}

void loop(){
  delay(1000);
}

Change that to

foo *ptr = &myFoo;

The difference is that it's initialized as part of the declaration, while what you did was add some executable code outside of a function.

You could also have done:

setup()
{
   ptr = &myfoo;
}

C/C++ isn't like javascript - you can't put executable code just anywhere! :wink:

2 Likes

you’re awesome, and working late… Thanks

1 Like