Search code examples
c++classobjectconstructoresp32

How to solve compiler error: no matching function for call to 'dmhFS::dmhFS()' in my case?


I write code for the ESP32 microcontroller.

I set up a class named "dmhWebServer".

This is the call to initiate my classes:

An object of the dmhFS class is created and I give it to the constructor of the dmhWebServer class by reference. For my error see the last code block that I posted. The other code block could explain the way to where the error shows up.

#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)
}

The class dmhFS has a custom constructor (header file, all good in here):

#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

Header file of the dmhWebServer class (not the whole thing):

public:
  dmhWebServer(dmhFS &fileSystem, dmhActivateBusy &activateBusyHandshake);
};

This is the dmhWebServer class:

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);
};

This is the constructor of the dmhWebServer class:

#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();
}

My compiler says:

src/dmhWebServer.cpp:5:85: error: no matching function for call to 'dmhFS::dmhFS()'

Line 5:85 is at the end of the constructor function declaration

This is my first question on StackOverflow since only lurking around here :) I try to clarify if something is not alright with the question.

I checked that I do the call by reference in C++ right. I am giving the constructor "dmhWebServer" what he wants.

What is the problem here?


Solution

  • In your dmhWebServer constructor, you are not initializing the abh and sharedFileSystem members in the constructor's member initialization list, so the compiler tries to default-initialize those members, but the dmhFS class does not have a default constructor, hence the error.

    You need to use the member initialization list for members with non-default constructors, eg:

    dmhWebServer::dmhWebServer(dmhFS &fileSystem, dmhActivateBusy &activateBusyHandshake)
        : sharedFileSystem(fileSystem), abh(activateBusyHandshake)
    {
        setupHandlers();
        server.begin();
    }