Call callback function of object in class

Hey guys,

I hope I can describe my problem understandable... :confused: I just made my first tries with callback functions and in an own class-example I made it work:

class c{
  public:
  float i(int data, int (* cbFunction)(int)) {
    return cbFunction(data);
  }  
};

int func(int var) {
  return var*3;
}

c c;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(c.i(5,&func)); // use "func" as callback function to call from c.i() --> works!
}

This was my first step to my actual target: Using a callback function that's a method within an object. Background: I created my own "framework" for my automatic plant care.
Now I came to the point that one sensor (DHT11) has its own library which simplifies the usage of the sensor. But of course I still need my own framework for all the other sensors and controls.

So this is the code to use the DHT11 sensor:

#include "DHT.h" //DHT Bibliothek laden

#define DHTPIN 2 //Der Sensor wird an PIN 2 angeschlossen    
#define DHTTYPE DHT11    // Es handelt sich um den DHT22 Sensor
DHT dht(DHTPIN, DHTTYPE); //Der Sensor wird ab jetzt mit „dth“ angesprochen


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  dht.begin(); //DHT22 Sensor starten
}

void loop() {
  delay(2000);
  float Luftfeuchtigkeit = dht.readHumidity(); //die Luftfeuchtigkeit auslesen und unter „Luftfeutchtigkeit“ speichern
  
  float Temperatur = dht.readTemperature();//die Temperatur auslesen und unter „Temperatur“ speichern
  
  Serial.print("Luftfeuchtigkeit: "); //Im seriellen Monitor den Text und 
  Serial.print(Luftfeuchtigkeit); //die Dazugehörigen Werte anzeigen
  Serial.println(" %");
  Serial.print("Temperatur: ");
  Serial.print(Temperatur);
  Serial.println(" Grad Celsius");
}

Of course that works properly, too.

Now the problematic step:

How can I call the method dht.readTemperature(); as callback from c.i()? Or rather how can I pass this callback to c.i()

I really appreciate every hint and help, because what I tried didn't work....

Thanks and regards!

class c{

  using FMPtr = float (DHT::*)(); //type aliasing: FMPtr is a member function of class DHT which returns a float and take no argument. 

  public:
  float i(int data, DHT& Obj, FMPtr Ptr) {
    return (Obj.*Ptr)(); //invoking the member function
  } 
};

...
DHT dht(DHTPIN, DHTTYPE); //Der Sensor wird ab jetzt mit „dth" angesprochen
void setup()
{
   c ClassC;
   ClassC.i(data, dht, dht.readTemperature);
}

For more information: Standard C++

Thanks for your code! Since I do not understand everything I have a lot to learn. :slight_smile:

But your code throws an error message I can’t fix, could you please help me again?

#include "DHT.h" //DHT Bibliothek laden

class c{

  using FMPtr = float (DHT::*)(); //type aliasing: FMPtr is a member function of class DHT which returns a float and take no argument. 

  public:
  float i(int data, DHT& Obj, FMPtr Ptr) {
    return (Obj.*Ptr)(); //invoking the member function
  } 
};



#define DHTPIN 2 //Der Sensor wird an PIN 2 angeschlossen    
#define DHTTYPE DHT11    // Es handelt sich um den DHT22 Sensor

DHT dht(DHTPIN, DHTTYPE); //Der Sensor wird ab jetzt mit „dth" angesprochen
void setup()
{
   c ClassC;
   ClassC.i(1, dht, dht.readTemperature); // THIS LINE THROWS THE ERROR MESSAGE
}

void loop() {
  // put your main code here, to run repeatedly:

}
Arduino: 1.8.11 (Mac OS X), Board: "Arduino Nano, ATmega328P (Old Bootloader)"

/Volumes/Daten/username/Documents/Nextcloud/Programmierung/Arduino/callback_function_dht11/callback_function_dht11.ino: In function 'void setup()':
callback_function_dht11:22:40: error: invalid use of non-static member function 'float DHT::readTemperature(bool, bool)'
    ClassC.i(1, dht, dht.readTemperature);
                                        ^
In file included from /Volumes/Daten/username/Documents/Nextcloud/Programmierung/Arduino/callback_function_dht11/callback_function_dht11.ino:1:0:
/Volumes/Daten/username/Documents/Nextcloud/Programmierung/Arduino/libraries/DHT_sensor_library/DHT.h:51:10: note: declared here
    float readTemperature(bool S=false, bool force=false);
          ^~~~~~~~~~~~~~~
exit status 1
invalid use of non-static member function 'float DHT::readTemperature(bool, bool)'

Replace:

using FMPtr = float (DHT::*)();

with

using FMPtr = float (DHT::*)(bool, bool);
return (Obj.*Ptr)(); //invoking the member function

throws this error message

/Volumes/Daten/username/Documents/Nextcloud/Programmierung/Arduino/callback_function_dht11/callback_function_dht11.ino: In member function 'float c::i(int, DHT&, c::FMPtr)':
callback_function_dht11:9:23: error: too few arguments to function
     return (Obj.*Ptr)(); //invoking the member function
                       ^
/Volumes/Daten/username/Documents/Nextcloud/Programmierung/Arduino/callback_function_dht11/callback_function_dht11.ino: In function 'void setup()':
callback_function_dht11:22:40: error: invalid use of non-static member function 'float DHT::readTemperature(bool, bool)'
    ClassC.i(1, dht, dht.readTemperature);
                                        ^
In file included from /Volumes/Daten/username/Documents/Nextcloud/Programmierung/Arduino/callback_function_dht11/callback_function_dht11.ino:1:0:
/Volumes/Daten/username/Documents/Nextcloud/Programmierung/Arduino/libraries/DHT_sensor_library/DHT.h:51:10: note: declared here
    float readTemperature(bool S=false, bool force=false);
          ^~~~~~~~~~~~~~~
exit status 1
too few arguments to function

so I replaced it with

return (Obj.*Ptr)(false, false); //invoking the member function

to have the correct number of arguments. Then the following error message is shown

/Volumes/Daten/username/Documents/Nextcloud/Programmierung/Arduino/callback_function_dht11/callback_function_dht11.ino: In function 'void setup()':
callback_function_dht11:22:40: error: invalid use of non-static member function 'float DHT::readTemperature(bool, bool)'
    ClassC.i(1, dht, dht.readTemperature);
                                        ^
In file included from /Volumes/Daten/username/Documents/Nextcloud/Programmierung/Arduino/callback_function_dht11/callback_function_dht11.ino:1:0:
/Volumes/Daten/username/Documents/Nextcloud/Programmierung/Arduino/libraries/DHT_sensor_library/DHT.h:51:10: note: declared here
    float readTemperature(bool S=false, bool force=false);
          ^~~~~~~~~~~~~~~
exit status 1
invalid use of non-static member function 'float DHT::readTemperature(bool, bool)'

Did you replace the type aliasing, FMPtr?

Also try add a &:

ClassC.i(1, dht, &(dht.readTemperature));

arduino_new:
Did you replace the type aliasing, FMPtr?

I did.

arduino_new:
Also try add a &:

ClassC.i(1, dht, &(dht.readTemperature));

I did and it compiles without errors now. I will try to use and implement it, thanks for your help so far. I hope I don’t need to come back, but let’s see… ;D