我曾经这样使用过coap服务器:
coapServer coap;
coap.server(callback_light, "light");
coap.start();以及回调方法:
void callback_light(coapPacket *packet, IPAddress ip, int port,int obs) {
///Some Work...
}而且它工作得很完美。我创建了一个名为COAPService的类和头文件:
#include <coap_server.h>
class COAPService
{
private:
coapServer coap;
int WiFiTimeOut = 5000;
void getListOfWiFi(coapPacket *packet, IPAddress ip, int port, int obs);//id = 0 GET
public:
COAPService();
void COAPLoop();
};和cpp文件:
#include "COAPService.h"
#include <coap_server.h>
#include <ESP8266WiFi.h>
COAPService::COAPService()
{
coap.server(static_cast<COAPService*>(this)->getListOfWiFi, "wifilist");
coap.start(5683);
}
void COAPService::getListOfWiFi(coapPacket *packet, IPAddress ip, int port, int obs) //id = 0 GET
{
///Some Work
}我的问题出在构造函数上。当我为callBack方法调用static_cast(this)->getListOfWiFi时,它返回:
COAPService.cpp:7:75: error: no matching function for call to 'coapServer::server(<unresolved overloaded function type>, const char [9])'
coap.server(static_cast<COAPService*>(this)->getListOfWiFi, "wifilist");为什么会出现这个错误?
发布于 2019-08-18 13:32:23
我假设问题是您将一个成员函数指针传递给coapServer,以便在将来的某个时候被调用。
如果这是正确的,我假设您作为参数传递给coap.server()的函数指针一定是一个非成员函数,因为您任何时候都没有传递对象指针。
尝试将getListOfWiFi转换为静态(在函数签名中包含关键字static ):
static void getListOfWiFi(coapPacket *packet, IPAddress ip, int port, int obs);或者只是在任何类/结构之外声明这个函数。
在COAPService ctor中,只需传递它的地址:
COAPService::COAPService()
{
// the static_cast you made here doesn't make much sense.
coap.server(getListOfWiFi, "wifilist");
coap.start(5683);
}https://stackoverflow.com/questions/57541848
复制相似问题