-->
Page 1 of 2

help with simple inheritance

PostPosted: Sun Mar 22, 2020 7:30 pm
by sorh
I was trying to add some custom methods to a base class using inheritance but I just can't get my head around this and it keeps failing compilation.
I'm probably making some basic stupid error, please have a look:

Code: Select all#include "FS.h"

class Filesystem : public FS {
public:
    void Filesystem::mount() {
        if (!Filesystem::begin())
        {
            Serial.println("Failed to mount file system");
        }
        else {
            Serial.println("File system mounted.");
        }
    }
}myFiles;

void setup() {
    Serial.begin(115200);
    Serial.println("Mounting FS...");
    myFiles.mount();
}

void loop() {
}


Compiler output:

Code: Select allCompiling 'filesystem' for 'Generic ESP8266 Module'
 
filesystem.ino: 6:10: error: extra qualification 'Filesystem::' on member 'mount' [-fpermissive]
     void Filesystem*: mount() {
 
filesystem.ino: 16:2: error: use of deleted function 'Filesystem::Filesystem()
   }myFile
filesystem.ino:4: note  Filesystem  Filesystem() is implicitly deleted because the default definition would be ill-formed
 class Filesystem *: public FS {
 
filesystem.ino: 4:7: error: no matching function for call to 'fs::FS::FS()
Error compiling project sources
filesystem.ino:4: note  candidates are
 
filesystem.ino:2: In file included from
FS.h:201: note  fs  FS  FS(fs  FSImplPtr)
     FS(FSImplPtr impl) *: _impl(impl) { timeCallback = _defaultTimeCB; }
FS.h:201: note    candidate expects 1 argument, 0 provided
FS.h:198: note  fs  FS  FS(const fs  FS&)
   class FS
FS.h:198: note    candidate expects 1 argument, 0 provided
FS.h:198: note  fs  FS  FS(fs  FS&&)
FS.h:198: note    candidate expects 1 argument, 0 provided
Build failed for project 'filesystem'

Re: help with simple inheritance

PostPosted: Mon Mar 23, 2020 1:11 am
by JurajA
first remove all occurrences of Filesystem:: from the class declaration

Re: help with simple inheritance

PostPosted: Mon Mar 23, 2020 9:28 am
by sorh
Thanks, but not much has changed:

Code: Select allCompiling 'filesystem' for 'Generic ESP8266 Module'
 
filesystem.ino: 14:2: error: use of deleted function 'Filesystem::Filesystem()
   }myFiles
filesystem.ino:3: note  Filesystem  Filesystem() is implicitly deleted because the default definition would be ill-formed
 class Filesystem *: public FS {
 
filesystem.ino: 3:7: error: no matching function for call to 'fs::FS::FS()
Error compiling project sources
Build failed for project 'filesystem'
filesystem.ino:3: note  candidates are
 
filesystem.ino:1: In file included from
FS.h:201: note  fs  FS  FS(fs  FSImplPtr)
     FS(FSImplPtr impl) *: _impl(impl) { timeCallback = _defaultTimeCB; }
FS.h:201: note    candidate expects 1 argument, 0 provided
FS.h:198: note  fs  FS  FS(const fs  FS&)
   class FS
FS.h:198: note    candidate expects 1 argument, 0 provided
FS.h:198: note  fs  FS  FS(fs  FS&&)
FS.h:198: note    candidate expects 1 argument, 0 provided


Re: help with simple inheritance

PostPosted: Mon Mar 23, 2020 11:50 am
by RichardS
Little lesson "simple" inheritance not always that simple :-)
https://stackoverflow.com/questions/366 ... onstructor

https://github.com/espressif/arduino-es ... c/FS.h#L89

Code: Select all#include "FS.h"
#include <SPIFFS.h>

class Filesystem : public FS {
  public:
    Filesystem() : FS(SPIFFS) {}; // because class FS has a constructor that needs a parameter it must be called here !!!see github link!!!
    void mount() {
      //if (begin()) {}// there is no member begin() in class FS
      if (open("filename", "r")) // but there is a member called open() so we try it here
      {
        Serial.println("Failed to mount file system");
      }
      else {
        Serial.println("File system mounted.");
      }
    }
};

void setup() {
  Serial.begin(115200);
  Serial.println("Mounting FS...");
  Filesystem myFiles;
  myFiles.mount();
}

void loop() {
}


Tested and compiles and works fine.

RichardS