Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Callback definition is incompatible

I use a library which has these definitions

typedef void (*CallbackFunction) (ESPRotary&);
void ESPRotary::setChangedHandler(CallbackFunction f) { ... }

When I try to use the setChangedHandler function I get an issue that the definition of my callback is wrong.

#pragma once

#include "ESPRotary.h"

class MyUsermod : public Usermod
{
private:
  ESPRotary r = ESPRotary(13, 12);

public:
  void rotate(ESPRotary &r)
  {
    Serial.println(r.getPosition());
  }

  void setup()
  {
    // argument of type "void (MyUsermod::*)(ESPRotary &r)" is incompatible 
    // with parameter of type "ESPRotary::CallbackFunction"C/C++(167)
    r.setChangedHandler(rotate); 
  }
};

What am I doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The callback function needs to be defined statically when defined inside the class:

class MyUsermod : public Usermod
{
private:
  ESPRotary r = ESPRotary(13, 12);

public:
  /* Callback function "static" defined */
  static void rotate(ESPRotary &r)
  {
    Serial.println(r.getPosition());
  }

  void setup()
  {
    r.setChangedHandler(rotate); 
  }
};
References
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading