我正在尝试使用Scheduler来使用ESP8266 WebSocketsServer实例,但我无法使用onEvent函数进行编译。在设置中,我正在调用
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:
...\Arduino\libraries\WebSockets\src/WebSocketsServer.h:58:14: note: void WebSocketsServer::onEvent(WebSocketsServer::WebSocketServerEvent)
void onEvent(WebSocketServerEvent cbEvent);
^
...\Arduino\libraries\WebSockets\src/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)>}'
你知道我做错了什么吗?
答案 0 :(得分:1)
std::bind()
将其辅助函数调用到原始函数时传递参数。根据函数中的参数数量,可能需要更多或更少的占位符。
要解决此问题,需要从
更改呼叫webSocket.onEvent(std::bind(&WebServer::webSocketEvent, this));
到
webSocket.onEvent(std::bind(&WebServer::webSocketEvent, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));