小编Sun*_*art的帖子

方法无法访问同一个类的成员变量(C++)

我写了一个简短的程序来说明我学校项目的继承原则,但我遇到了一个奇怪的问题.这是我的代码:(我省略了所有不是问题的代码)

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)

我不知道这个错误意味着什么,我希望有人可以帮助我.

c++ static class protected

5
推荐指数
1
解决办法
1990
查看次数

标签 统计

c++ ×1

class ×1

protected ×1

static ×1