1

I have a function that takes the following argument

void func(void (*otherFunc)());

and I have a class constrat like this (.h file):

class DataReciver {
public:
    DataReciver(uint8_t pinClk, uint8_t pinDIO);
    void onReciveData();
    uint8_t readData();

private:
    uint8_t m_pinClk;
    uint8_t m_pinDIO;
    uint8_t m_bitIndex;
    uint8_t m_byteData;
};

In the DataReciver constrator creation in the .cpp file, I want to pass the this->onReciveData function to func, Something like func(this->onReciveData).
When I tried that, I recived an error "a pointer to a bound function may only be used to call the function".

Is that posible somehow to pass that function to func? Thank you!

Note - I can not rewrite the func function to take another arguments, it is given to me by someone else library.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • If `func()` is as shown in your post, then you won't be able to do what you're describing. Is that the exact signature for `func()`? If not, can you post the exact signature? – scg Oct 25 '20 at 03:13
  • @scg That is the full signature: `void attachInterrupt(uint8_t interruptNum, void (*userFunc)(), int mode)` – עתודה אקדמאית Oct 25 '20 at 03:16
  • When in the example I gave, `attachInterrupt` called `func` and `userFunc` called `otherFunc`. – עתודה אקדמאית Oct 25 '20 at 03:17
  • I see. Sometimes libraries that use callbacks like this will allow you to 'bind' something via e.g. a 'user data' void pointer, but that doesn't appear to be the case here. In any case, I think the exact same problem you're having is discussed [here](https://forum.arduino.cc/index.php?topic=398610.0). I didn't scrutinize that thread and can't vouch for its contents, but in any case, one way or another you'll need to pass a function that isn't a non-static member function. – scg Oct 25 '20 at 03:26

0 Answers0