Sun*_*art 5 c++ static class protected
我写了一个简短的程序来说明我学校项目的继承原则,但我遇到了一个奇怪的问题.这是我的代码:(我省略了所有不是问题的代码)
class Car
{
protected:
double fuelLevel;
public:
void fuelUp(double);
};
void fuelUp(double fuel)
{
Car::fuelLevel += fuel;
}
Run Code Online (Sandbox Code Playgroud)
这是构建日志:
||=== 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)) ===|
Run Code Online (Sandbox Code Playgroud)
我不知道这个错误意味着什么,我希望有人可以帮助我.
该函数应该写成类的成员 Car
void Car::fuelUp(double fuel)
{
fuelLevel += fuel;
}
Run Code Online (Sandbox Code Playgroud)
你编写它的方式,它不能访问任何成员变量,Car因为它是一个与你在类中声明的函数不同的函数.