bool Car::isEnoughFuel(int miles) const
{
drivableMiles = fuelGauge.getCurrentFuel() * MPG; // Error here
bool status = true;
if (miles > drivableMiles)
status = false;
return status;
}
Error: Expression must be a modifiable lvalue.
bool Car::isEnoughFuel(int miles) const
{
drivableMiles = fuelGauge.getCurrentFuel() * MPG; // Error here
bool status = true;
if (miles > drivableMiles)
status = false;
return status;
}
Error: Expression must be a modifiable lvalue.
Declaring a member function with the
const
keyword specifies that the function is a "read-only" function that does not modify the object for which it is called. A constant member function cannot modify any non-static data members or call any member functions that aren't constant. (Source)
In the line drivableMiles = fuelGauge.getCurrentFuel() * MPG;
you are trying to modify the object isEnoughFuel()
is called on. You probably don't want to have your function as const
. However one work around would be to using copies instead.