Arduino SocketIoClient-没有用于调用的匹配函数

时间:2018-10-14 10:44:03

标签: c++ arduino bind

我正在尝试使用Here中的SocketIoClient。当我在main.cpp文件中这样做时,一切正常。但是,当我尝试在自己的类中使用webSocket时会出错。

//.h 
#include <SocketIoClient.h>
class SocketClient
{
   public:
     SocketClient();
     void setup();
 private:
     SocketIoClient webSocket;
     void handleData(const char * payload, size_t length);
};

//.cpp file:
#include "SocketClient.h"

SocketClient::SocketClient(){

}
void SocketClient::setup(){
  webSocket.on("data", std::bind(&SocketClient::handleData, this));
}
void SocketClient::handleData(const char * payload, size_t length) {
    Serial.print("handleData (" + String(length) + "):");
    Serial.println(payload);
}

我正在获取此堆栈跟踪:

error: no matching function for call to 'SocketIoClient::on(const char [10], std::_Bind_helper<false, void (SocketClient::*)(const char*, unsigned int), SocketClient* const>::type)'
webSocket.on("stageData", std::bind(&SocketClient::handleData, this));
^
lib\socket.io-client-master/SocketIoClient.h:41:7: note: void SocketIoClient::on(const char*, std::function<void(const char*, unsigned int)>)
void on(const char* event, std::function<void (const char * payload, size_t length)>);
^
lib\socket.io-client-master/SocketIoClient.h:41:7: note:   no known conversion for argument 2 from 'std::_Bind_helper<false, void (SocketClient::*)(const char*, unsigned int), SocketClient* const>::type {aka std::_Bind<std::_Mem_fn<void (SocketClient::*)(const char*, unsigned int)>(SocketClient*)>}' to 'std::function<void(const char*, unsigned int)>'

这怎么在我班上工作?

这是webSocket.on函数:

void on(const char* event, std::function<void (const char * payload, size_t length)>);

1 个答案:

答案 0 :(得分:1)

您必须使用i来告知 您的成员函数需要2个参数,这些参数 应该取消绑定,因为由创建的函数对象 0 <= i <= 63将由std::placeholders::X对象和该对象触发 会在调用时为回调提供参数。

std::bind

webSocket的方法中的某处:

webSocket

callback = bind(,_1,_2); // create callback