An issue about lvalue error

When I compile my code the compiler shows me this message.
using temporary as lvalue [-fpermissive]
sensors.get(i).runningMedian = ds18b20.getTempCByIndex(i);

sensors is a linke liste that store sensor which is struct. runningMedian is the property/field inside the struct.
I don’t know why this happen. This problem doesn’t show up when the sensor was created and I was trying to assign the value to it, but the code after that start happen problem.

I imagine your sensors.get(i) is returning a struct or class of some kind. This will create a temporary variable since it’s got to be stored somewhere.

Better to rewrite code to be more explicit:

Sensor sensor = sensors.get(i);
sensor.runningMedian = ...;

Note that if you’re returning the sensor by value, then changing the fields will have no affect, since you’re changing them on a temporary variable.

Instead, make the get() function return a reference, by adding a & after the return type name, e.g.

Change

Sensor SensorsClass::get(int index);

to

Sensor& SensorsClass::get(int index);

That will then make the code function the way you expect.

3 Likes