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

Unresolved overloaded function type when rewriting code c++

本文关键字:重载 函数 类型 代码 c++ 重写      更新时间:2023-10-16

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

提供回调函数(管理请求(时出现的错误

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

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

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

目数.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;
}

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

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