使用成员函数作为回调

时间:2016-01-11 08:58:07

标签: c++ lambda

我想将方法​​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)'

2 个答案:

答案 0 :(得分:0)

您不能将指向成员函数的指针作为函数参数的简单指针传递,因为指向成员函数的指针需要在有效的类实例上调用回调。

参数类型

void(*)(WStype_t type, uint8_t * payload, size_t length)

不允许

void (Connection::*)(WStype_t type, uint8_t* payload, size_t length)

这是指向Connection::onWebSocketEvent的指针的类型,并且lambda无法转换为函数指针,除非它没有捕获任何内容。

这里的麻烦是你在wsConnection的类型之间得到某种循环依赖关系,如果你想要ws的类型,事情会开始变得不安1}}要了解当前的Connection实例。

答案 1 :(得分:0)

试试这个:

using namespace std::placeholders;

...

this->ws.onEvent(std::bind(&Connection::onWebSocketEvent, this, _1, _2, _3))