Particle function from class object

Can someone help me with the following: I want to have a class containing a function and variable which are accessible from the cloud. But with the following code I get the error:

filename.ino:21:40: invalid use of non-static member function

The used code:

#include "application.h"

class example {
    public:
        example();
        int var;
        int set(String);
};

example::example() {
    var = 0;
}

int example::set(String command) {
    var = command.toInt();
}

example object;

void setup() {
    Particle.function("set", object.set);
    Particle.variable("var", object.var);
}

void loop() {

}

Thank you

The address of a non-static class field is only its offset in respect to the object, but in order to dereference such an offset you’d also need the address of the object itself.

But the docs show how it’s done right

// EXAMPLE USAGE WITH C++ OBJECT

class CoffeeMaker {
  public:
    CoffeeMaker() {
      Particle.function("brew", &CoffeeMaker::brew, this);
    }

    int brew(String command) {
      // do stuff
      return 1;
    }
};

CoffeeMaker myCoffeeMaker;
// nothing else needed in setup() or loop()
2 Likes

Perfect! Thank you very much!

1 Like