[英]Using member function as a callback
我想将方法Connection::onWebSocketEvent
作为参数传递给this->ws.onEvent()
,但它不起作用。 我是 C++ 的新手。 我究竟做错了什么?
void Connection::connect(String host, int port, String fingerprint, String token) {
Serial.println(String("Connect to wss://")+host+":"+port+" with token "+token);
this->host = host;
this->port = port;
this->fingerprint = fingerprint;
this->token = token;
this->ws.protocol = "io-json-v1";
this->ws.beginSSL(this->host, this->port, "/channel", this->fingerprint);
// attempt 1
// this->ws.onEvent(std::bind(&Connection::onWebSocketEvent, this));
// attempt 2
this->ws.onEvent([&](WStype_t type, uint8_t* payload, size_t length) {
this->onWebSocketEvent(type, payload, length);
});
}
void Connection::onWebSocketEvent(WStype_t type, uint8_t* payload, size_t length) {
// ...
}
WebSocketsClient::onEvent
的方法头是:
typedef void (*WebSocketClientEvent)(WStype_t type, uint8_t * payload, size_t length);
void onEvent(WebSocketClientEvent cbEvent);
在我第一次尝试时,我得到了这个错误:
IO.cpp: In member function 'void Connection::connect(String, int, String, String)':
IO.cpp:16: error: no matching function for call to 'WebSocketsClient::onEvent(std::_Bind_helper<false, void (Connection::*)(WStype_t, unsigned char*, unsigned int), Connection* const>::type)'
this->ws.onEvent(std::bind(&Connection::onWebSocketEvent, this));
^
IO.cpp:16:66: note: candidate is:
In file included from IO.h:7:0,
from IO.cpp:1:
/Users/chris/Documents/Arduino/libraries/arduinoWebSockets/src/WebSocketsClient.h:50:14: note: void WebSocketsClient::onEvent(WebSocketsClient::WebSocketClientEvent)
void onEvent(WebSocketClientEvent cbEvent);
^
/Users/chris/Documents/Arduino/libraries/arduinoWebSockets/src/WebSocketsClient.h:50:14: note: no known conversion for argument 1 from 'std::_Bind_helper<false, void (Connection::*)(WStype_t, unsigned char*, unsigned int), Connection* const>::type {aka std::_Bind<std::_Mem_fn<void (Connection::*)(WStype_t, unsigned char*, unsigned int)>(Connection*)>}' to 'WebSocketsClient::WebSocketClientEvent {aka void (*)(WStype_t, unsigned char*, unsigned int)}'
no matching function for call to 'WebSocketsClient::onEvent(std::_Bind_helper<false, void (Connection::*)(WStype_t, unsigned char*, unsigned int), Connection* const>::type)'
在第二个:
IO.cpp: In member function 'void Connection::connect(String, int, String, String)':
IO.cpp:20: error: no matching function for call to 'WebSocketsClient::onEvent(Connection::connect(String, int, String, String)::__lambda0)'
});
^
IO.cpp:20:4: note: candidate is:
In file included from IO.h:7:0,
from IO.cpp:1:
/Users/chris/Documents/Arduino/libraries/arduinoWebSockets/src/WebSocketsClient.h:50:14: note: void WebSocketsClient::onEvent(WebSocketsClient::WebSocketClientEvent)
void onEvent(WebSocketClientEvent cbEvent);
^
/Users/chris/Documents/Arduino/libraries/arduinoWebSockets/src/WebSocketsClient.h:50:14: note: no known conversion for argument 1 from 'Connection::connect(String, int, String, String)::__lambda0' to 'WebSocketsClient::WebSocketClientEvent {aka void (*)(WStype_t, unsigned char*, unsigned int)}'
no matching function for call to 'WebSocketsClient::onEvent(Connection::connect(String, int, String, String)::__lambda0)'
您不能将指向成员函数的指针作为指向函数参数的简单指针传递,因为指向成员函数的指针需要在有效的类实例上调用回调。
参数类型
void(*)(WStype_t type, uint8_t * payload, size_t length)
不允许
void (Connection::*)(WStype_t type, uint8_t* payload, size_t length)
这是指向Connection::onWebSocketEvent
的指针类型,lambda 不能转换为函数指针,除非它不捕获任何内容。
这里的问题是,如果您希望ws
的类型知道当前的Connection
实例,您会在ws
的类型和Connection
之间产生某种循环依赖,并且事情开始变得不安。
试试这个:
using namespace std::placeholders;
...
this->ws.onEvent(std::bind(&Connection::onWebSocketEvent, this, _1, _2, _3))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.