2
我想通過方法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)'
[傳遞lambda作爲函數指針]可能的重複(http://stackoverflow.com/questions/28746744/passing-lambda-as-function-pointer) – YSC
你可能改變'onEvent'函數簽名?否則,如果您需要動態數量的實例,您可能會失敗。 – grek40