Skip to main content
1 of 7
Faultier
  • 143
  • 1
  • 7

pass class internal function as callback

I am trying to pass a class internal function as a callback function within the class. The error behavior is similar to this problem. Whatsoever, I was unable to construct working code based on that question's answer.

I try to pass over a callback function from within the class,in order to create a finite state machine, inspired by the examples I found on adafruit regarding multithreading. The idea is, that each state can call his successor. The class itself then represents the state machine and I just will call its update function.

While the following code does not resemble this utility, it resembles the problem in a minimal example (based on this larger codepiece):

class MyClass
{
  const byte theAnswer = 42;
  void (*myCallback)();

  void setCallback(void (*callback)())
  {
    myCallback = callback;
  }
  
  public:
  
  void print_info()
  {
    Serial.println(theAnswer);
  }

  void call()
  {
    myCallback();
  }
  
   MyClass()
  {
    setCallback(&MyClass::print_info);
  }
};


MyClass AClassOfItsOwn();

void setup() 
{
  Serial.begin(9600);
  while (! Serial); // Wait until Serial is ready
}

void loop() 
{
  AClassOfItsOwn.call();
  return;
}

I get the following error code returned:

...testscript.ino: In constructor 'MyClass::MyClass()':

testscript:25: error: no matching function for call to 'MyClass::setCallback(void (MyClass::*)())'

     setCallback(&MyClass::print_info);

                                     ^

...\testscript.ino:25:41: note: candidate is:

...\testscript.ino:6:12: note: void MyClass::setCallback(void (*)())

   void setCallback(void (*callback)())

        ^

...\testscript.ino:6:12: note: no known conversion for argument 1 from 'void (MyClass::)()' to 'void ()()'

...\testscript.ino: In function 'void loop()':

testscript:40: error: request for member 'call' in 'AClassOfItsOwn', which is of non-class type 'MyClass()'

   AClassOfItsOwn.call();

                  ^

exit status 1 no matching function for call to 'MyClass::setCallback(void (MyClass::*)())'

Faultier
  • 143
  • 1
  • 7