[英]Using member function as a callback
I want to pass the method Connection::onWebSocketEvent
as an argument to this->ws.onEvent()
, but it doesn't work.我想将方法Connection::onWebSocketEvent
作为参数传递给this->ws.onEvent()
,但它不起作用。 I'm new to C++.我是 C++ 的新手。 What am I doing wrong?我究竟做错了什么?
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) {
// ...
}
The method header for WebSocketsClient::onEvent
is: WebSocketsClient::onEvent
的方法头是:
typedef void (*WebSocketClientEvent)(WStype_t type, uint8_t * payload, size_t length);
void onEvent(WebSocketClientEvent cbEvent);
On my first attempt I get this error:在我第一次尝试时,我得到了这个错误:
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)'
On the second:在第二个:
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)'
You cannot pass a pointer to member function as a simple pointer to function argument since a pointer to member function requires the callback to be called on a valid class instance.您不能将指向成员函数的指针作为指向函数参数的简单指针传递,因为指向成员函数的指针需要在有效的类实例上调用回调。
The argument type参数类型
void(*)(WStype_t type, uint8_t * payload, size_t length)
does not permit不允许
void (Connection::*)(WStype_t type, uint8_t* payload, size_t length)
which is the type of a pointer to Connection::onWebSocketEvent
and a lambda cannot convert to a function pointer unless it does not capture anything.这是指向Connection::onWebSocketEvent
的指针类型,lambda 不能转换为函数指针,除非它不捕获任何内容。
The trouble here is that you'd get some kind of a circular dependency between the type of ws
and Connection
and things start to get uneasy, if you want the type of ws
to be aware of the current Connection
instance.这里的问题是,如果您希望ws
的类型知道当前的Connection
实例,您会在ws
的类型和Connection
之间产生某种循环依赖,并且事情开始变得不安。
Try this one:试试这个:
using namespace std::placeholders;
... ...
this->ws.onEvent(std::bind(&Connection::onWebSocketEvent, this, _1, _2, _3))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.