Skip to main content
edited body
Source Link
jfpoilpret
  • 9.2k
  • 7
  • 38
  • 54

The right way, in C++, to properly construct a field member b (of type class B) inside a class A is to do it as follows in BA's constructor:

A::A() : b(...) {
    // Other init code here
}

In your situation that would mean:

class ScaleMeasure
{
 public:
    ScaleMeasure(int Dout, int Clk, float CalibF, int ScaleID); 
    void Setup();   
    float Measure();    

private:
    int _dout_pin;
    int _clk_pin;
    float _calibration_factor;
    byte _scaleID;
    HX711 _scale;  
};

Note that I have removed the init part for _scale.

ScaleMeasure::ScaleMeasure(int Dout, int Clk, float CalibF, int ScaleID)
: _scale(Dout, CLk)
{
    _clk_pin = Clk;
    _dout_pin = Dout;   
    _calibration_factor = CalibF;
    _scaleID = ScaleID;     
}

The right way, in C++, to properly construct a field member b (of type class B) inside a class A is to do it as follows in B's constructor:

A::A() : b(...) {
    // Other init code here
}

In your situation that would mean:

class ScaleMeasure
{
 public:
    ScaleMeasure(int Dout, int Clk, float CalibF, int ScaleID); 
    void Setup();   
    float Measure();    

private:
    int _dout_pin;
    int _clk_pin;
    float _calibration_factor;
    byte _scaleID;
    HX711 _scale;  
};

Note that I have removed the init part for _scale.

ScaleMeasure::ScaleMeasure(int Dout, int Clk, float CalibF, int ScaleID)
: _scale(Dout, CLk)
{
    _clk_pin = Clk;
    _dout_pin = Dout;   
    _calibration_factor = CalibF;
    _scaleID = ScaleID;     
}

The right way, in C++, to properly construct a field member b (of type class B) inside a class A is to do it as follows in A's constructor:

A::A() : b(...) {
    // Other init code here
}

In your situation that would mean:

class ScaleMeasure
{
 public:
    ScaleMeasure(int Dout, int Clk, float CalibF, int ScaleID); 
    void Setup();   
    float Measure();    

private:
    int _dout_pin;
    int _clk_pin;
    float _calibration_factor;
    byte _scaleID;
    HX711 _scale;  
};

Note that I have removed the init part for _scale.

ScaleMeasure::ScaleMeasure(int Dout, int Clk, float CalibF, int ScaleID)
: _scale(Dout, CLk)
{
    _clk_pin = Clk;
    _dout_pin = Dout;   
    _calibration_factor = CalibF;
    _scaleID = ScaleID;     
}
Source Link
jfpoilpret
  • 9.2k
  • 7
  • 38
  • 54

The right way, in C++, to properly construct a field member b (of type class B) inside a class A is to do it as follows in B's constructor:

A::A() : b(...) {
    // Other init code here
}

In your situation that would mean:

class ScaleMeasure
{
 public:
    ScaleMeasure(int Dout, int Clk, float CalibF, int ScaleID); 
    void Setup();   
    float Measure();    

private:
    int _dout_pin;
    int _clk_pin;
    float _calibration_factor;
    byte _scaleID;
    HX711 _scale;  
};

Note that I have removed the init part for _scale.

ScaleMeasure::ScaleMeasure(int Dout, int Clk, float CalibF, int ScaleID)
: _scale(Dout, CLk)
{
    _clk_pin = Clk;
    _dout_pin = Dout;   
    _calibration_factor = CalibF;
    _scaleID = ScaleID;     
}