我为 ESP32 微控制器编写代码。
我设置了一个名为“dmhWebServer”的类。
这是开始我的课程的电话:
创建了 dmhFS 类的对象,并将其通过引用传递给 dmhWebServer 类的构造函数。对于我的错误,请参阅我发布的最后一个代码块。另一个代码块可以解释错误出现的方式。
#include <dmhFS.h>
#include <dmhNetwork.h>
#include <dmhWebServer.h>
void setup()
{
// initialize filesystems
dmhFS fileSystem = dmhFS(SCK, MISO, MOSI, CS); // compiler is happy I have an object now
// initialize Activate Busy Handshake
dmhActivateBusy activateBusy = dmhActivateBusy();
// initialize webserver
dmhWebServer webServer(fileSystem, activateBusy); // compiler also happy (call by reference)
}
类 dmhFS 有一个自定义构造函数(头文件,都在这里):
#include <Arduino.h>
#include <SD.h>
#include <SPI.h>
#include <LittleFS.h>
#include <dmhPinlist.h>
#ifndef DMHFS_H_
#define DMHFS_H_
class dmhFS
{
private:
// serial peripheral interface
SPIClass spi;
String readFile(fs::FS &fs, const char *path);
void writeFile(fs::FS &fs, const char *path, const char *message);
void appendFile(fs::FS &fs, const char *path, const char *message);
void listDir(fs::FS &fs, const char *dirname, uint8_t levels);
public:
dmhFS(uint16_t sck, uint16_t miso, uint16_t mosi, uint16_t ss);
void writeToSDCard();
void saveData(std::string fileName, std::string contents);
String readFileSDCard(std::string fileName);
};
#endif
dmhWebServer 类的头文件(不是全部):
public:
dmhWebServer(dmhFS &fileSystem, dmhActivateBusy &activateBusyHandshake);
};
这是 dmhWebServer 类:
class dmhWebServer
{
private:
// create AsyncWebServer object on port 80
AsyncWebServer server = AsyncWebServer(80);
// server to client communication
AsyncEventSource events = AsyncEventSource("/events");
void setupHandlers();
void setupWebServer();
void serveFiles();
void setupStaticFilesHandlers();
void setupEventHandler();
void setupPostHandler();
void onRequest(AsyncWebServerRequest *request);
// http communication
void sendToClient(const char *content, const char *jsEventName);
void receiveFromClient(std::array<const char *, 2U> par);
// uses SD Card
dmhFS sharedFileSystem;
// uses shared memory for data exchange with a activate busy handshake
dmhActivateBusy abh;
public:
dmhWebServer(dmhFS &fileSystem, dmhActivateBusy &activateBusyHandshake);
};
这是 dmhWebServer 类的构造函数:
#include <dmhWebServer.h>
#include <dmhFS.h>
#include <dmhActivateBusy.h>
// This is the line where the compiler throws an error ,character 85 is ")"
dmhWebServer::dmhWebServer(dmhFS &fileSystem, dmhActivateBusy &activateBusyHandshake)
{
// webserver sites handlers
setupHandlers();
abh = activateBusyHandshake;
sharedFileSystem = fileSystem;
// start web server, object "server" is instantiated as private member in header file
server.begin();
}
我的编译器说:
src/dmhWebServer.cpp:5:85:错误:没有调用“dmhFS::dmhFS()”的匹配函数
第 5:85 行位于构造函数声明的末尾
这是我在 StackOverflow 上的第一个问题,因为只潜伏在这里:)我试图澄清这个问题是否有问题。
我检查了我是否正确地在 C++ 中通过引用进行了调用。我给了构造函数“dmhWebServer”他想要的东西。
这里有什么问题?
在您的
dmhWebServer
构造函数中,您没有初始化构造函数的 成员初始化列表中的
abh
和 sharedFileSystem
成员,因此编译器会尝试 默认初始化 这些成员,但 dmhFS
类会这样做没有默认构造函数,因此出现错误。
需要使用成员初始化列表,eg:
dmhWebServer::dmhWebServer(dmhFS &fileSystem, dmhActivateBusy &activateBusyHandshake)
: sharedFileSystem(fileSystem), abh(activateBusyHandshake)
{
setupHandlers();
server.begin();
}