The way that you have it written Car::fuelLevel += fuel
is triyng to access the variable fuelLevel as if it were static
. You need to either make that variable static
or, more likely what you meant to do, is make the method
void Car::fuelUp(double fuel)
{
fuelLevel += fuel;
}
If it makes it any more clear why your original code was wrong, you could also change it to:
void Car::fuelUp(double fuel)
{
this->fuelLevel += fuel;
}
Notice in the second variant that you are accessing the field via this->
, which is implicitly happening in the first version. Your version had it accessing the field via the class Car::
.