A method can't access a member variable of the same class (C++)
c++, class, protected, static
Solution
That function should be written as a member of the class `Car`
void Car::fuelUp(double fuel)
{
fuelLevel += fuel;
}
The way you wrote it, it does not have access to any of the member variables in `Car` because it is a different function than the one you declared in the class.
Problem
I wrote a short program to illustrate the principles of inheritance for my school project, but I am having a weird problem. Here is my code: (I have omitted all the code that isn't the problem) ``` class Car { protected: double fuelLevel; public: void fuelUp(double); }; void fuelUp(double fuel) { Car::fuelLevel += fuel; } ``` and this is the build log: ``` ||=== Build: Debug in wierdError (compiler: GNU GCC Compiler) ===| ||In function 'void fuelUp(double)':| |4|error: 'double Car::fuelLevel' is protected| |11|error: within this context| |4|error: invalid use of non-static data member 'Car::fuelLevel'| |11|error: from this location| ||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| ``` I have no idea about what this error means and I hope there is somebody who can help me.