SdFat Object as a protected member of the class

Problem

I am trying to use a SdFat object as a protected or private class member, which I had working previously, but the SdFat library has not updated that I can tell since my last successful implementation of this firmware.

Previously, I would just pass the SdFat reference to the constructor my myClass and set the member of the class equal to the SdFat reference, and this would work no issues.

Code

myClass.h

#ifndef myClass_h
#define myClass_h
#include "Particle.h"
#include "SdFat.h"

class myClass
{
    protected:
         SdFat &_sdCard;
         uint8_t _chipSelect;
    private: 
         // many class methods are here to be called from inside the class
    public:
         myClass(SdFat &sdFat, const uint8_t cs) : _sdCard(sdFat), _chipSelect(cs) {};
}


#endif

firmware.ino

#include <SPI.h>
#include "SdFat.h"
#include "Particle.h"
#include "myClass.h"

SYSTEM_THREAD(ENABLED);

void setup()
{
    if (!sdCard.begin(chipSelect, SPI_FULL_SPEED))
    {
        Serial.println("Failed to initialize sd card");
    }
	
    myClass myClassObject(sdCard, chipSelect);
}

Expectations v. Reality

Expectaitons

This has previously worked for me, but now I see an error from SdFat that the = operator is no longer supported? Hence this constructor notation I am using, I found this inspecting one of @rickkas7 libraries where he used an SdFat object inside of a class right here. This functionality is what I would like to achieve

Reality

This code results in a hard fault every time I call a method on the SdCard object, and I am almost certain this is because of the Calling an invalid pointer scenario.

Any help would be appreciated, thanks!

I’d guess that it’s the change in compiler version caused by using a newer Device OS target version that’s causing the error.

One possible solution that comes to mind is switching to passing the SdFat object as a pointer (SdFat *) instead of a reference (SdFat &). Copying the reference should not call the copy operator, so that seems like it should have worked, but apparently not.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.