Can't use library objects inside a class

Hi,

I researched this issue for a while now, but I can't seem to get any of the found solutions to work. I have an Arduino sketch that runs an underwater thruster test jig, controlling an ESC, and measuring the force using a load cell. I am using two standard libraries for this - Servo.h and HX711_ADC.h. In my sketch, I created an object for each of these library classes, and it worked fine. However, when I wrapped them inside of a custom class, I get compilation errors. The first one is:

C:\Users\Administrator\AppData\Local\Temp\.arduinoIDE-unsaved2023217-18000-exa87x.ufjn\sketch_mar17a\sketch_mar17a.ino: In constructor 'testInstance::testInstance()':
C:\Users\Administrator\AppData\Local\Temp\.arduinoIDE-unsaved2023217-18000-exa87x.ufjn\sketch_mar17a\sketch_mar17a.ino:15:5: error: no matching function for call to 'HX711_ADC::HX711_ADC()'

Here is a trimmed code that shows my setup:

  #include <Servo.h>
  #include <HX711_ADC.h>

class testInstance
{
  private:
    const int HX711_DOUT = 4; //mcu > HX711 dout pin
    const int HX711_SCK = 5; //mcu > HX711 sck pin

    Servo ESC;
    HX711_ADC LoadCell;

  public:
    bool invertThrottle = false;
    
    testInstance()
    {
      LoadCell = HX711_ADC(HX711_DOUT, HX711_SCK);
      ESC = Servo();
    }

    void runTest()
    {
      //...
    } 
};


void setup() 
{
  Serial.begin(9600); 
  //forward
  testInstance forwardTest;
  forwardTest.runTest();

  //reverse
  testInstance reverseTest;
  reverseTest.invertThrottle = true;
  forwardTest.runTest();
}

I also get more errors, all referring to these objects I created. The full error list is this:

C:\Users\Administrator\AppData\Local\Temp\.arduinoIDE-unsaved2023217-18000-exa87x.ufjn\sketch_mar17a\sketch_mar17a.ino: In constructor 'testInstance::testInstance()':
C:\Users\Administrator\AppData\Local\Temp\.arduinoIDE-unsaved2023217-18000-exa87x.ufjn\sketch_mar17a\sketch_mar17a.ino:18:5: error: no matching function for call to 'HX711_ADC::HX711_ADC()'
     {
     ^
In file included from C:\Users\Administrator\AppData\Local\Temp\.arduinoIDE-unsaved2023217-18000-exa87x.ufjn\sketch_mar17a\sketch_mar17a.ino:2:0:
C:\Users\Administrator\Documents\Arduino\libraries\HX711_ADC-1.2.12\src/HX711_ADC.h:53:3: note: candidate: HX711_ADC::HX711_ADC(uint8_t, uint8_t)
   HX711_ADC(uint8_t dout, uint8_t sck);   //constructor
   ^~~~~~~~~
C:\Users\Administrator\Documents\Arduino\libraries\HX711_ADC-1.2.12\src/HX711_ADC.h:53:3: note:   candidate expects 2 arguments, 0 provided
C:\Users\Administrator\Documents\Arduino\libraries\HX711_ADC-1.2.12\src/HX711_ADC.h:49:7: note: candidate: constexpr HX711_ADC::HX711_ADC(const HX711_ADC&)
 class HX711_ADC
       ^~~~~~~~~
C:\Users\Administrator\Documents\Arduino\libraries\HX711_ADC-1.2.12\src/HX711_ADC.h:49:7: note:   candidate expects 1 argument, 0 provided
C:\Users\Administrator\Documents\Arduino\libraries\HX711_ADC-1.2.12\src/HX711_ADC.h:49:7: note: candidate: constexpr HX711_ADC::HX711_ADC(HX711_ADC&&)
C:\Users\Administrator\Documents\Arduino\libraries\HX711_ADC-1.2.12\src/HX711_ADC.h:49:7: note:   candidate expects 1 argument, 0 provided
C:\Users\Administrator\AppData\Local\Temp\.arduinoIDE-unsaved2023217-18000-exa87x.ufjn\sketch_mar17a\sketch_mar17a.ino:19:49: error: use of deleted function 'HX711_ADC& HX711_ADC::operator=(HX711_ADC&&)'
       LoadCell = HX711_ADC(HX711_DOUT, HX711_SCK);
                                                 ^
In file included from C:\Users\Administrator\AppData\Local\Temp\.arduinoIDE-unsaved2023217-18000-exa87x.ufjn\sketch_mar17a\sketch_mar17a.ino:2:0:
C:\Users\Administrator\Documents\Arduino\libraries\HX711_ADC-1.2.12\src/HX711_ADC.h:49:7: note: 'HX711_ADC& HX711_ADC::operator=(HX711_ADC&&)' is implicitly deleted because the default definition would be ill-formed:
 class HX711_ADC
       ^~~~~~~~~
C:\Users\Administrator\Documents\Arduino\libraries\HX711_ADC-1.2.12\src/HX711_ADC.h:49:7: error: non-static const member 'const uint8_t HX711_ADC::divBitCompiled', can't use default assignment operator

exit status 1

Compilation error: no matching function for call to 'HX711_ADC::HX711_ADC()'

What am I doing wrong here?

These are both wrong. The second one is not needed at all. Get rid of it. For the HX711_ADC object you must use an Initializer List. See Case #3 Here.

Thank you for your answer. I read that article prior to posting this question, and attempted to implement what was explained there, but I must be doing something wrong. Following that #3 case, I wrote my code like this now:

  #include <Servo.h>
  #include <HX711_ADC.h>

class testInstance
{
  private:
    const int HX711_DOUT = 4; //mcu > HX711 dout pin
    const int HX711_SCK = 5; //mcu > HX711 sck pin


    Servo ESC;
    HX711_ADC LoadCell;

  public:
    bool invertThrottle = false;

    testInstance()
    {
      LoadCell = HX711_ADC(HX711_DOUT, HX711_SCK);
    }

    void runTest()
    {
      //...
    } 
};

testInstance::testInstance(uint8_t dout, uint8_t sck):LoadCell(dout, sck)

void setup() 
{
  Serial.begin(9600); 
  //forward
  testInstance forwardTest;
  forwardTest.runTest();

  //reverse
  testInstance reverseTest;
  reverseTest.invertThrottle = true;
  forwardTest.runTest();
}

And I get the exact same errors, nothing changed. Why is that initializer list needed at all? What does it do? And why it isn't required to have ESC = Servo(); in my class anymore, but it is needed if this object is instantized outside of the class? How can it even be instantized if it doesn't have () ? I apologize if these are stupid questions, but I honestly spent several days reading on the initializer lists, and I still don't get it.

EDIT: by the way, I am not sure if this is the right case, since it seems to me that this library does have a constructor. This was taken from HX711_ADC.h:

class HX711_ADC
{	
		
	public:
		HX711_ADC(uint8_t dout, uint8_t sck); 		//constructor
		void setGain(uint8_t gain = 128); 			//value must be 32, 64 or 128*
		void begin();								//set pinMode, HX711 gain and power up the HX711
		void begin(uint8_t gain);					//set pinMode, HX711 selected gain and power up the HX711

That first public member does seem like a constructor to me...

You're still not using an initializer list as described in the article I linked. See code below.

Because that's the syntax required by the C++ language's rules.

No, it's not. In fact, I find that syntax ugly. Just define the object as you would any other variable (such as 'int') ... Type variableName;

Servo ESC;

BTW, your variable name 'ESC' while not an error goes against the convention of using camelCase for variable names.

It is the constructor. And it's not a default (no argument) constructor. So, you must use an initializer list as decribed in Case 3 of the article I linked.

So:

#include <Servo.h>
#include <HX711_ADC.h>

class TestInstance
{
  private:
    const int HX711_DOUT = 4; //mcu > HX711 dout pin
    const int HX711_SCK = 5; //mcu > HX711 sck pin

    Servo esc;
    HX711_ADC loadCell;

  public:
    TestInstance() : loadCell(HX711_DOUT, HX711_SCK) {}
    bool invertThrottle = false;

    void runTest()    {
      //...
    }
};

void setup() {
  Serial.begin(115200);
  //forward
  TestInstance forwardTest;
  forwardTest.runTest();

  //reverse
  TestInstance reverseTest;
  reverseTest.invertThrottle = true;
  reverseTest.runTest();
}

void loop() {
}