Multiple classes in classes

Hey guys,

I'm quite new in the Arduino world and I'm coming from PHP where things seem to be a bit more easy. So I have a problem with this:

I have two classes. One class should be instantiated in the second one but I only get an error message:

This class is instantiated first

class config{
  public:
  sensorLimit temperatureSensorLimits;

  void setTemperatureLimit(float temp){
    this->temperatureSensorLimits->limitLow = temp;
  }

  float getTemperatureLimit() {
    return this->temperatureSensorLimits->limitLow;
  }

};

and that's the class that should be instantiated in the first one

class sensorLimit{
  public:
  float limitLow;
  float limitHigh;
};

But compiling doesn't work and throws this error

base operand of '->' has non-pointer type 'sensorLimit'

How can I instantiate a class in a class and access the properties / methods?

I'm using Arduino IDE 1.8.11.

Thanks and best regards
Stefan

class sensorLimit;

class config
{
 ...
}

class sensorLimit
{
   ...
}

To get proper assistance, post a complete code.

Sorry, but I got the error on the line with

this->temperatureSensorLimits->limitLow = temp;

so I thought this was enough...

Here's the whole code

class sensorLimit{
  public:
  float limitLow;
  float limitHigh;
};

class config{
  public:
  sensorLimit temperatureSensorLimits;

  void setTemperatureLimit(float temp){
    this->temperatureSensorLimits->limitLow = temp;
  }

  float getTemperatureLimit() {
    return this->temperatureSensorLimits->limitLow;
  }

};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  config Config;
}

void loop() {
  // put your main code here, to run repeatedly:
  Config.setTemperatureLimit(23.0);
  Serial.println(Config.getTemperatureLimit());
}
class sensorLimit {
  public:
    float limitLow;
    float limitHigh;
};

class config {
  public:
    sensorLimit temperatureSensorLimits;

    void setTemperatureLimit(float temp) {
      temperatureSensorLimits.limitLow = temp;
    }

    float getTemperatureLimit() {
      return temperatureSensorLimits.limitLow;
    }

};

config Config;
void setup() {
  Serial.begin(115200);
  Config.setTemperatureLimit(23.0);
  Serial.println(Config.getTemperatureLimit());
}

void loop() {
}

I increased the Serial port's baud because it's 2020.

Seems odd that you would make temperatureSensorLimits a public data member and then provide getter and setter functions. The OOP tenant of Encapsulation would suggest making it private.

I know it's probably the "correct" syntax, but using "this->" always seemed to cause too much clutter when I write classes. You can just call the class member directly without compile or runtime errors. That might simplify things for you.

Also, can you post the compile error?

Try to replace "->" (pointer reference) with "." (static reference):

// This line:
this->temperatureSensorLimits->limitLow = temp;

//Becomes this:
temperatureSensorLimits.limitLow = temp;

Power_Broker:
Also, can you post the compile error?

Here's the compile error:

Arduino: 1.8.11 (Mac OS X), Board: "Arduino Nano, ATmega328P (Old Bootloader)"

/var/folders/b5/qc8dstcn02v_hyvgxsq4w9vr0000gq/T/arduino_modified_sketch_21180/sketch_jan19b.ino: In member function 'void config::setTemperatureLimit(float)':
sketch_jan19b:12:34: error: base operand of '->' has non-pointer type 'sensorLimit'
     this->temperatureSensorLimits->limitLow = temp;
                                  ^~
/var/folders/b5/qc8dstcn02v_hyvgxsq4w9vr0000gq/T/arduino_modified_sketch_21180/sketch_jan19b.ino: In member function 'float config::getTemperatureLimit()':
sketch_jan19b:16:41: error: base operand of '->' has non-pointer type 'sensorLimit'
     return this->temperatureSensorLimits->limitLow;
                                         ^~
/var/folders/b5/qc8dstcn02v_hyvgxsq4w9vr0000gq/T/arduino_modified_sketch_21180/sketch_jan19b.ino: In function 'void loop()':
sketch_jan19b:29:3: error: 'Config' was not declared in this scope
   Config.setTemperatureLimit(23.0);
   ^~~~~~
/var/folders/b5/qc8dstcn02v_hyvgxsq4w9vr0000gq/T/arduino_modified_sketch_21180/sketch_jan19b.ino:29:3: note: suggested alternative: 'config'
   Config.setTemperatureLimit(23.0);
   ^~~~~~
   config
exit status 1
base operand of '->' has non-pointer type 'sensorLimit'

Dieser Bericht wäre detaillierter, wenn die Option
"Ausführliche Ausgabe während der Kompilierung"
in Datei -> Voreinstellungen aktiviert wäre.

Danois90:
Try to replace "->" (pointer reference) with "." (static reference):

// This line:

this->temperatureSensorLimits->limitLow = temp;

//Becomes this:
temperatureSensorLimits.limitLow = temp;

Perfect, thank you!! Is there a limit of how many objects / dimensions I can use "interlaced"?

alve89:
Perfect, thank you!! Is there a limit of how many objects / dimensions I can use "interlaced"?

Huh? You can expand space and compress the gas giants and maybe cause a sun to flare.. Or something in Beijing.. Maybe.. Dunno..?