在类中引用函数时的"No Matching Function for Call"

"No Matching Function for Call" when referencing function in class

本文关键字:No Matching for Call Function 引用 函数      更新时间:2023-10-16

我正在尝试使ESP8266 WebSocketServer实例与调度程序一起使用,但是我无法通过一个vent函数进行编译。在设置中,我正在调用

webSocket.onEvent(std::bind(&WebServer::webSocketEvent, this));

WebServer是类,webSocketEvent定义为

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {...}

这是完整的编译错误日志:

sketch_nov20a.ino: In member function 'virtual void WebServer::setup()':
sketch_nov20a:25: error: no matching function for call to 'WebSocketsServer::onEvent(std::_Bind_helper<false, void (WebServer::*)(unsigned char, WStype_t, unsigned char*, unsigned int), WebServer* const>::type)'
       webSocket.onEvent(std::bind(&WebServer::webSocketEvent, this));
                                                                    ^
sketch_nov20a.ino:25:68: note: candidate is:
In file included from sketch_nov20a.ino:3:0:
...ArduinolibrariesWebSocketssrc/WebSocketsServer.h:58:14: note: void WebSocketsServer::onEvent(WebSocketsServer::WebSocketServerEvent)
         void onEvent(WebSocketServerEvent cbEvent);
              ^
...ArduinolibrariesWebSocketssrc/WebSocketsServer.h:58:14: note:   no known conversion for argument 1 from 'std::_Bind_helper<false, void (WebServer::*)(unsigned char, WStype_t, unsigned char*, unsigned int), WebServer* const>::type {aka std::_Bind<std::_Mem_fn<void (WebServer::*)(unsigned char, WStype_t, unsigned char*, unsigned int)>(WebServer*)>}' to 'WebSocketsServer::WebSocketServerEvent {aka std::function<void(unsigned char, WStype_t, unsigned char*, unsigned int)>}'

您知道我在做什么错吗?

std::bind()需要明确告知从辅助手机的辅助手术到原始函数时的参数。根据功能中的参数数量,可能需要或多或少的占位符。

要解决此问题,需要从

更改呼叫
webSocket.onEvent(std::bind(&WebServer::webSocketEvent, this));

to

webSocket.onEvent(std::bind(&WebServer::webSocketEvent, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
相关文章: