<unresolved overloaded function type> with callbacks

Hi,

I’m not much into C++, but trying to learn a bit with classes.
So I’m doing my project the class/lib way.
I’m using Platform.IO.

Seems going ok so far, but I have a problem with callbacks from a class fonction.

this code in HandleButton.h file, included from main.c, don’t compile:

src/HandleButton.h: error: no matching function for call to 'PushButton::onButtonPressed(<unresolved overloaded function type>)'
#include <PushButton.h>
#include <Button.h>
#include <ButtonEventCallback.h>
#include <Bounce2.h>  
#include "defines.h"

class HandleButton {
  private:
    byte _pinBut;
    byte _typeBut;
    unsigned int _but_rst = 8000; // Time to hold before resetting config & esp
    unsigned int _but_act = 1000; // time to hold before trap action
    
    unsigned long _nextChangeTime = 0;
    PushButton _button = PushButton(_pinBut,_typeBut);

   // Callback


    void onButtonPressed(Button& btn){

        Serial.println("button pressed");
    }

  public:
    HandleButton(byte _pinBut, byte _typeBut) {
        this->_pinBut = _pinBut;
        this->_typeBut = _typeBut;
 
        pinMode(_pinBut, INPUT);
        
    }
    
    // setup
    void begin() {

        _button.onPress(onButtonPressed); 

    }

    // main loop
    void loop() {
        _button.update();
    }

    
};

Error is related to this line:

  _button.onPress(onButtonPressed);

I feel it’s somethingrelated to bad pointer to OnButtonPressed function from the class.

If somebody can ELI5 why such error, that would be much appreciated.

Thanks.

C doesn't have classes. Try renaming it main.cpp.

Posting some complete Arduino code that actually compiled, or failed to compile, with EXACT messages, would be the first step.

setup() and loop() are not supposed to be class members.

I'm using Platform.IO.

Why? For what?

pert:
C doesn’t have classes. Try renaming it main.cpp.

My mistake misspelled it, it’s already named main.cpp

PaulS:
Posting some complete Arduino code that actually compiled, or failed to compile, with EXACT messages, would be the first step.

setup() and loop() are not supposed to be class members.
Why? For what?

I use Platform.io to manage, edit & build my projects. Fit better than arduino IDE to me.

So here is a light example:

main.cpp:

#include <Arduino.h>

#include <HandleButton.h>


HandleButton handleButton = HandleButton(5,B0);

void setup() {

    handleButton.begin();

}

void loop() {

  handleButton.loop();
}

and HandleButton.h

#include <PushButton.h>
#include <Button.h>
#include <ButtonEventCallback.h>
#include <Bounce2.h>  

class HandleButton {
  private:
    byte _pinBut;
    byte _typeBut;
    unsigned int _but_rst = 8000; // Time to hold before resetting config & esp
    unsigned int _but_act = 1000; // time to hold before trap action
    
    unsigned long _nextChangeTime = 0;
    PushButton _button = PushButton(_pinBut,_typeBut);

   // Callback


    void onButtonPressed(Button& btn){

        Serial.println("button pressed");
    }

  public:
    HandleButton(byte _pinBut, byte _typeBut) {
        this->_pinBut = _pinBut;
        this->_typeBut = _typeBut;
 
        pinMode(_pinBut, INPUT);
        
    }
    
    // setup
    void begin() {

        _button.onPress(onButtonPressed); 

    }

    // main loop
    void loop() {
        _button.update();
    }

    
};

Compile output gives:

Compiling .pioenvs\nodemcuv2\src\main.cpp.o
Generating LD script .pioenvs\nodemcuv2\ld\eagle.app.v6.common.ld
Compiling .pioenvs\nodemcuv2\lib1a3\r89m Buttons_ID315\BasicButton\BasicButton.cpp.o
Compiling .pioenvs\nodemcuv2\lib1a3\r89m Buttons_ID315\Button\Button.cpp.o
Compiling .pioenvs\nodemcuv2\lib1a3\r89m Buttons_ID315\Button\ButtonEventCallback.cpp.o
In file included from src\main.cpp:4:0:
src/HandleButton.h: In member function 'void HandleButton::begin()':
src/HandleButton.h:37:40: error: no matching function for call to 'PushButton::onPress(<unresolved overloaded function type>)'
_button.onPress(onButtonPressed);
^
src/HandleButton.h:37:40: note: candidate is:
In file included from .piolibdeps\r89m PushButton_ID314\src/PushButton.h:13:0,
from src/HandleButton.h:1,
from src\main.cpp:4:
.piolibdeps\r89m Buttons_ID315\src/Button.h:56:8: note: void Button::onPress(ButtonOnPressCallback)
void onPress(ButtonOnPressCallback);
^
.piolibdeps\r89m Buttons_ID315\src/Button.h:56:8: note:   no known conversion for argument 1 from '<unresolved overloaded function type>' to 'ButtonOnPressCallback {aka void (*)(Button&)}'
*** [.pioenvs\nodemcuv2\src\main.cpp.o] Error 1

There are many problems with your code that you need to fix.

First, the implementation of the class should NOT be done in the header file. Move the implementation to a cpp file.

Second, you can NOT instantiate an object in the class definition. You can only declare that a variable has a specific type.

Third, you should NOT call pinMode() from the constructor. When the constructor is called, you do NOT know whether the hardware is ready to be diddled with. Global constructors are called BEFORE init(), which gets the hardware ready.

Finally, you should test compilation using the IDE, to see if it is a problem with your code or with your build process.

Thanks PaulS.

So I’ve cleaned up this following your comments. It helped me to get few things I wasn’t confident with, thanks for that.

Still the same error btw ( was intended, I pretty feel my problem is linked to a pointer thing around the callback )

new code :

HandleButton.h:

#include <PushButton.h>
#include <Button.h>
#include <ButtonEventCallback.h>
#include <Bounce2.h>  

class HandleButton {
  private:
    byte _pinBut;
    byte _typeBut;
    unsigned int _but_rst = 8000; // Timbe to hold before resetting config & esp
    unsigned int _but_act = 1000; // time to hold before trap action   
    unsigned long _nextChangeTime = 0;
    
    void configurePushButton(Bounce& bouncedButton);
    void onButtonPressed(Button& btn);
    void onButtonHeld(Button& btn, uint16_t duration, uint16_t repeatCount);
    void onButtonReleased(Button& btn, uint16_t duration);

  public:
    HandleButton(byte _pinBut, byte _typeBut);   
    void begin();
    void loop();
};

HandleButton.cpp:

#include <HandleButton.h>

PushButton *_button;

HandleButton::HandleButton(byte _pinBut, byte _typeBut) {
        this->_pinBut = _pinBut;
        this->_typeBut = _typeBut;
        _button = new PushButton(_pinBut,_typeBut);
}

void HandleButton::configurePushButton(Bounce& bouncedButton){
    // Set the debounce interval to 15ms - default is 10ms
    bouncedButton.interval(10);
}

void HandleButton::onButtonPressed(Button& btn){
    Serial.println("button pressed");
}

void HandleButton::onButtonHeld(Button& btn, uint16_t duration, uint16_t repeatCount){
    if (duration >=  _but_rst) {
        Serial.println(F("BUTTON RESET CONF & REBOOT"));
        yield();      
        // Delete config & Reboot
        // TOdo deleteConfig();
        ESP.reset();
        delay(5000);        
    }
}

// duration reports back the total time that the button was held down
void HandleButton::onButtonReleased(Button& btn, uint16_t duration){
    if (duration <= _but_act) {
        //if (traped) initTrap();
        //else closeTrap();
        Serial.println("BUTTON: Change position");
    }
}

    
void HandleButton::begin() {
    pinMode(_pinBut, INPUT);
    _button->configureButton(configurePushButton);
    _button->onPress(onButtonPressed);
    _button->onHoldRepeat(1000, 500, onButtonHeld);
    _button->onRelease(onButtonReleased);  
}

void HandleButton::loop() {
    _button->update();
}

main.cpp unchanged

Same error:

src\HandleButton.cpp:44:37: error: no matching function for call to 'PushButton::onPress(<unresolved overloaded function type>)'
_button->onPress(onButtonPressed);
^
src\HandleButton.cpp:44:37: note: candidate is:
In file included from .piolibdeps\r89m PushButton_ID314\src/PushButton.h:13:0,
from src/HandleButton.h:1,
from src\HandleButton.cpp:1:
.piolibdeps\r89m Buttons_ID315\src/Button.h:56:8: note: void Button::onPress(ButtonOnPressCallback)
void onPress(ButtonOnPressCallback);
^
.piolibdeps\r89m Buttons_ID315\src/Button.h:56:8: note:   no known conversion for argument 1 from '<unresolved overloaded function type>' to 'ButtonOnPressCallback {aka void (*)(Button&)}'
src\HandleButton.cpp:45:50: error: no matching function for call to

I’ve copied the error only for OnPress , but same occurs for the other ones of course.

Also, this compile & work fine in the .ino way:

test.ino:

#include <PushButton.h>
#include <Button.h>
#include <ButtonEventCallback.h>
#include <Bounce2.h>    // https://github.com/thomasfredericks/Bounce-Arduino-Wiring

#define BUT_RESET     10000
#define BUT_CONFIG    5000
#define BUT_DOOR      2000


PushButton button = PushButton(BUTTONPIN,PRESSED_WHEN_LOW);

void setup() {
	pinMode(BUTTONPIN,INPUT);
	button.configureButton(configurePushButton);
	button.onPress(onButtonPressed);
	button.onHoldRepeat(1000, 500, onButtonHeld);
	button.onRelease(onButtonReleased);  
}
void loop() {
	button.update();
}
void configurePushButton(Bounce& bouncedButton){

        // Set the debounce interval to 15ms - default is 10ms
        bouncedButton.interval(10);
}

void onButtonPressed(Button& btn){

  Serial.println("button pressed");

}

void onButtonHeld(Button& btn, uint16_t duration, uint16_t repeatCount){

  if (duration >= BUT_RESET) {
      Serial.println(F("SYS: RESET CONF & REBOOT"));
    
  }
}

// duration reports back the total time that the button was held down
void onButtonReleased(Button& btn, uint16_t duration){
  if (duration <= BUT_DOOR) {
		Serial.println("Button Released: Open door")
	}
  
  else if (duration >= BUT_CONFIG && duration < BUT_RESET) {
      Serial.println(F("Button Released: Starting portal"));
    
  }
}

I’ve found what was wrong.
As I attached the callback function to an object from another class, I need to make it static.

static void configurePushButton(Bounce& bouncedButton);

solved the problem.

Thanks for your help, indirectly, it helped. :slight_smile: