簡體   English   中英

使用成員函數作為回調

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM