2016-01-11 97 views
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)' 
+0

[傳遞lambda作爲函數指針]可能的重複(http://stackoverflow.com/questions/28746744/passing-lambda-as-function-pointer) – YSC

+0

你可能改變'onEvent'函數簽名?否則,如果您需要動態數量的實例,您可能會失敗。 – grek40

回答

0

你不能傳遞一個指向成員函數的指針作爲函數參數的簡單指針,因爲指向成員函數的指針需要在有效的類實例上調用回調函數。

參數類型

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

不允許

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

這是一個指針的類型Connection::onWebSocketEvent和拉姆達不能轉化爲函數指針,除非它沒有捕獲任何東西。

這裏的問題是,你會得到某種wsConnection和事情開始變得不安,如果你想要的ws類型要知道當前Connection實例的類型之間的循環依賴的。