1

Class myIOT2 contains a function that need to be called from class IPmonitoring:

bool myIOT2::checkInternet(char *externalSite, byte pings)
{
    return Ping.ping(externalSite, pings);
}

This function is needed as a pinger in IPmonitor:

void IPmonitoring::start(cb_func ping)
{
    _ping_cb = ping;

}

and this cb_func is defined by (inside IPmonitor.h):

typedef bool (*cb_func)(char *externalSite, byte pings);
cb_func _ping_cb;

inside my sketch (relevant parts only) I fail when iot.checkInternet is passed, while using a function test that calls iot.checkInternet succeeds:

myIOT2 iot;
IPmonitoring WiFi_service(ROUTER_IP, "Router");

bool test(char *a, byte b) { bool x = iot.checkInternet(ROUTER_IP); return x; }

void setup() { WiFi_service.start(iot.checkInternet); // <---- Trial a)fails WiFi_service.start(test); // <-----Trail b)succeeds }

error is :

home/guy/Documents/git/Arduino/IPmonitor/IPmonitor.ino: In function 'void setup()':
IPmonitor:436:45: error: no matching function for call to 'IPmonitoring::start(<unresolved overloaded function type>)'
         WiFi_service.start(iot.checkInternet);//
                                             ^
/home/guy/Documents/git/Arduino/IPmonitor/IPmonitor.ino:436:45: note: candidate is:
In file included from /home/guy/Documents/git/Arduino/IPmonitor/IPmonitor.ino:2:0:
/home/guy/Documents/git/Arduino/libraries/myIPmonitor/myIPmonitor.h:42:10: note: void IPmonitoring::start(IPmonitoring::cb_func)
     void start(cb_func ping);
          ^
/home/guy/Documents/git/Arduino/libraries/myIPmonitor/myIPmonitor.h:42:10: note:   no known conversion for argument 1 from '<unresolved overloaded function type>' to 'IPmonitoring::cb_func {aka bool (*)(char*, unsigned char)}'
Multiple libraries were found 

What is done wrong ? and why test succeeds ?

guyd
  • 1,049
  • 2
  • 26
  • 61

1 Answers1

1

There is a fundamental difference between a simple function and a function that is a member of a class. You can't use a function pointer to point to a member function of a class: simply because that member function also includes information about the class instance that it belongs to.

The proper C++ way to handle it is to use std::function, but the Arduino AVR compiler lacks the full STL (Standard Template Library), so that can't be used on normal Arduinos. Other cores do have it, so you may well be able to use it on the ESP8266.

If you want to make your code portable between systems that may not have the full STL then there's other tricks you have to do:

  • Use a wrapper function (which as you have found works), or
  • Make a polymorphic class and store a pointer to the instance cast to the parent class - the parent class defines the functions that are allowed to be called from the pointer.
Majenko
  • 105,851
  • 5
  • 82
  • 139