Cannot read the value from a class variable using getter function

31 Views Asked by At

I have a class which has a private variable. To access that from the main function I created a getter and setter methods.

class Sensor {
private:
  ...
  int data;
  ...
public:
  Sensor()
  {
    data=0;
  }
  void setData(int payload) {
    data = payload;
  }

  int getData() {
    return data
  }

};

My class is given above. In the main function ..

Sensor aqiSensor;

int main() {
  

  while(true) 
  {
    readSensorFunction(aqiSensor);
    
    printf("AQI: %d", aqiSensor.getData());

  }


}

void readSensorFunction(Sensor sen) 
{
  int rawData;
  int data;
  ... // rawData to data logic

  sen.setData(data);
  printf("read data: %d", sen.getData());
}

The issue I am facing is that The printf in the readSensorFunction prints the data stored in the class variable correctly. But the printf in the main function is not printing the data stored in the class variable. main function is printing it as 0 instead.

What could be the problem?

0

There are 0 best solutions below