重写代码c ++时未解决的重载函数类型

时间:2017-07-20 15:52:08

标签: c++ mesh

我在我的项目中使用ESP8266WiFiMesh库,我在nodeMCU上运行。当库提供的示例在Ardunino ino文件中时,它运行良好,但是当我将其重写为c ++ cpp h文件时,我遇到了一些问题。在Mesh :: Mesh中,当我创建ESP8266WiFiMesh类的实例时,我提供了两个参数:chipId和callback。

提供回调函数(manageRequest)

时出错
<unresolved overloaded function type> to std::function<String(String)>

我挣扎了几天,我找不到任何解决方案。

这是我使用的原始示例代码。 https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFiMesh/examples/HelloMesh/HelloMesh.ino

Mesh.cpp:

#include "Mesh.h"
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMesh.h>

unsigned int request_i = 0;
unsigned int response_i = 0;

Mesh::Mesh() {
    /* Create the mesh node object */
    /* Here I'm getting Unresolved overloaded function when providing manageRequest function (callback) */
    ESP8266WiFiMesh mesh_node = ESP8266WiFiMesh(ESP.getChipId(), manageRequest);

    /* Create the utils node object */
    utilsy = new Utils();

#ifdef DEBUG
    Serial.println("Setting up mesh node...");
#endif

    /* Initialize the mesh node */
    mesh_node.begin();
}

/**
* Callback for when other nodes send you data
*
* @request The string received from another node in the mesh
* @response The string to send back to the other node
*/
String Mesh::manageRequest(String request) {

    /* Split request */
    String requestedTo = utilsy->getDelimitedValues(request, '.', 0);
    String requestedFrom = utilsy->getDelimitedValues(request, '.', 1);
    const char *cstr = requestedFrom.c_str();
    String requestedMessage = utilsy->getDelimitedValues(request, '.', 2);
    String requestedState = utilsy->getDelimitedValues(request, '.', 3);

#ifdef DEBUG
    /* Print out received message */
    Serial.print("Requested from: " + requestedFrom);
    Serial.print("\tMessage: " + requestedMessage);
    Serial.println("\tRequested state: " + requestedState);
#endif

    /* return a string to send back */
    char response[60];
    //TODO should send back with same message and giving proper state [0,1] whether LED turned on/off
    sprintf(response, "%s.%d.%d.%d", cstr, ESP.getChipId(), response_i++, 1);
    return response;
}

1 个答案:

答案 0 :(得分:0)

代码不起作用,因为您的函数是一个类方法,但您可以使用std::bind来获得适当的结果:

ESP8266WiFiMesh mesh_node = ESP8266WiFiMesh(
    ESP.getChipId(),
    std::bind(&Mesh::manageRequest, this, std::placeholders::_1));