function pointers as parameter to other function

Viewed 115

I have a class in which there is a member function that takes as argument a function pointer, just like this:

//pinBuilder.hh
class pinBuilder
{
private:

public:
    void setMODER(const gpioMode &mode, bool(*callback)(const pinBuilder&) = nullptr);
    void setMODER();
};

This is the implementation:

//pinBuilder.cpp

bool myFunction(pinBuilder &obj){ }
bool (*myFunctionPtr)(pinBuilder &obj) = &myFunction;

void pinBuilder::setMODER(const gpioMode &mode, bool (*callback)(const pinBuilder &obj))
{
    if(callback != nullptr)
    {
        (callback(*this));
    }
    this->_instance->MODER &= ~(uint32_t)(GPIO_MODER_MODE0_Msk << (static_cast<int>(_params->pin) * 2U ));
    this->_instance->MODER |= (uint32_t)(static_cast<unsigned int>(mode) << (static_cast<int>(_params->pin) * 2U ));
}
void pinBuilder::setMODER()
{
    setMODER(_params->mode, myFunctionPtr(*this)); /*<- ERROR IS HERE ->*/
}

I'm getting a compile time error that goes like:

error: no matching function for call to 'pinBuilder::setMODER(const gpioMode&, bool)'
   48 |     setMODER(_params->mode, myFunctionPtr(*this));
note:   no known conversion for argument 2 from 'bool' to 'bool (*)(const pinBuilder&)'
   37 | void pinBuilder::setMODER(const gpioMode &mode, bool (*callback)(const pinBuilder &obj))

From what I could understand from the compiler, in this line here:

setMODER(_params->mode, myFunctionPtr(*this));

myFunctionPtr is taken as a boolean value instead of a pointer function returning a boolean.

I really don't have a clue what am I doing wrong, if someone could enlighten me, I would really appreciate it.

Thanks in advance

0 Answers
Related