+ 1

Function pointer for templated class method

Hi everyone, I need a hand to solve this problem, I'm trying to create a function that allows me to call a class method passed as a parameter of the aforementioned function, only that once I compile the code I get this error: src\main.cpp: In function 'void setup()': src\main.cpp:38:37: error: invalid use of non-static member function 'void esp8266webserver::ESP8266WebServerTemplate<ServerType>::handleClient() [with ServerType = WiFiServer]' 38 | b25.handler.when_connected(server.handleClient); | ~~~~~~~^~~~~~~~~~~~ In file included from ..\..\..\..\.platformio\packages\framework-arduinoespressif8266\libraries\ESP8266WebServer\src/ESP8266WebServer.h:340, from src\main.cpp:4: ..\..\..\..\.platformio\packages\framework-arduinoespressif8266\libraries\ESP8266WebServer\src/ESP8266WebServer-impl.h:274:6: note: declared here 274 | void ESP8266WebServerTemplate<ServerType>::handleClient() { | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1st Feb 2025, 9:56 PM
Mick
Mick - avatar
21 Respostas
+ 1
I'm working with esp8266 board in PlatformIO in VSCode. The line b25.handler.when_connected(callbackfunction_t callback); where typedef void(*callbackfunction_t)(void);
1st Feb 2025, 9:57 PM
Mick
Mick - avatar
+ 1
I want to make the function pointer work without modifying the Esp8266WebServer class.
2nd Feb 2025, 3:00 PM
Mick
Mick - avatar
+ 1
Yes, I'm making a similar one.
2nd Feb 2025, 3:26 PM
Mick
Mick - avatar
0
These lines together generate the error described above.
1st Feb 2025, 9:58 PM
Mick
Mick - avatar
0
Mick , I don't know much about C++, but it'd be helpful if you link us to your code, like this: https://sololearn.com/compiler-playground/c9XEgLk7ipsR/?ref=app And, you can send us a screenshot of your error by sending us a link to your screenshot, which can be done using this website: https://imgur.com/
2nd Feb 2025, 6:55 AM
Dragon RB
Dragon RB - avatar
0
I hope you'll receive useful answers! I'm sorry I can't help you since I'm not a C++ developer.
2nd Feb 2025, 7:16 AM
Dragon RB
Dragon RB - avatar
0
code?
2nd Feb 2025, 11:38 AM
Bob_Li
Bob_Li - avatar
0
The method I am trying to pass as a parameter to this function is on line 109 of this library handleClient(); link: https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WebServer/src/ESP8266WebServer.h
2nd Feb 2025, 11:45 AM
Mick
Mick - avatar
0
// orvibo_handler.h: typedef void(*callbackfunction_t)(void); // orvibo_handler.cpp: void OrviboHandler::when_connected(callbackfunction_t callback) { /* type: callbackfunction_t */ when_connected_function_pointer = callback; }
2nd Feb 2025, 11:48 AM
Mick
Mick - avatar
0
Code that generate the error: //main.cpp: b25.handler.when_connected(server.handleClient);
2nd Feb 2025, 11:49 AM
Mick
Mick - avatar
0
without seeing the code you wrote or knowing how your project is set up or how your build system is configured, it's hard to tell where the problem is. if it's your first time doing this, maybe start from simple tests and progress from that. have you tried compiling the test example first to see if your setup runs ok? https://github.com/platformio/platform-espressif8266/blob/6ee41e971e3834667d41ffc6839eb392ec611ad5/examples/arduino-webserver/src/HelloServer.ino
2nd Feb 2025, 1:01 PM
Bob_Li
Bob_Li - avatar
0
Yes, the example test works great, I just have this problem. Like I can't use functionpointer_t with methods in classes that use the template keyword.
2nd Feb 2025, 2:31 PM
Mick
Mick - avatar
0
I didn't touch anything in the compiler configuration, I used the default settings for the esp8266 board with PlatformIO on VSCode
2nd Feb 2025, 2:32 PM
Mick
Mick - avatar
0
invalid use of non-static member function ... maybe you first have to create an instance of the esp8266WebServer class and use that? just a wild guess ...
2nd Feb 2025, 2:57 PM
Bob_Li
Bob_Li - avatar
0
The project is not on github yet, I am evaluating whether to continue it or not. I created an instance of the class, it is called b25 using: OrviboB25 b25; statement.
2nd Feb 2025, 2:59 PM
Mick
Mick - avatar
0
There is nothing static about the OrviboB25 class
2nd Feb 2025, 3:00 PM
Mick
Mick - avatar
0
maybe some other members can give a more specific feedback. It's been years since I've played with these things, unfortunately. have you visited this? https://github.com/sandysound/orvibo-b25-server
2nd Feb 2025, 3:06 PM
Bob_Li
Bob_Li - avatar
0
If you had to store the pointer address of the server.handleClient(); method in a variable, how would you do it? Then call it inside the OrviboB25 class.
2nd Feb 2025, 3:28 PM
Mick
Mick - avatar
0
Have you tried creating an instance of the WiFiServer, then pointing to that instead of the base class? Also, another link I found https://github.com/xoseperez/espurna
2nd Feb 2025, 3:44 PM
Bob_Li
Bob_Li - avatar
0
What do you mean ?
2nd Feb 2025, 3:45 PM
Mick
Mick - avatar