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 ?

6
  • Does this answer your question? Passing a method pointer – Juraj Apr 13 at 17:11
  • @Juraj No :( I didn't – Guy . D Apr 13 at 17:31
  • @Juraj - mostly because Passing test succeeds – Guy . D Apr 13 at 17:45
  • did you read that answer to the end? – Juraj Apr 13 at 17:47
  • 2
    the core of the problem is that a class method compiled has the object as first parameter so the parameters of the callback function don't match for cb_func – Juraj Apr 13 at 18:13
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.
2
  • Thank you very much for a comprehensive explanation. A) it is an ESP8366. B) wrapper function is needed for which one my member functions? C) can you please write or direct for such example ? I must say it is a bit vague for me. – Guy . D Apr 14 at 4:24
  • 1
    The "wrapper function" is a simple function that calls the function in the object you want. Your "test" function from your example. Something like void test() { myObject.someFunction(); } - I use something similar here – Majenko Apr 14 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.