错误变量受保护

Error Variable is Protected

    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <ctime>
    using namespace std;

    void armySkirmish();
    void battleOutcome();

    string commander = "";
    int numberOfHumans = 0;
    int numberOfZombies = 0;

    class ArmyValues
    {
        protected:
            double attackPower;
            double defensePower;
            double healthPoints;
    public:
        void setAttackPower(double a)
        {
            attackPower = a;
        }
        void setDefensePower(double d)
        {
            defensePower = d;
        }
        void setHealthPoints(double h)
        {
            healthPoints = h * (defensePower * .1);
        }
};

class Zombies: public ArmyValues
{

};

class Humans: public ArmyValues
{

};

int main(int argc, char ** argv)
{
    cout << "Input Commander's Name: " << endl;
    cin >> commander;

    cout << "Enter Number of Human Warriors: " << endl;
    cin >> numberOfHumans;

    cout << "Enter Number of Zombie Warriors: " << endl;
    cin >> numberOfZombies;

    armySkirmish();
    battleOutcome();
    return 0;
}

void armySkirmish()
{

    cout << "\nThe Humans tense as the sound of the undead shuffle towards them." << endl;
    cout << commander << " shuffles forward with a determined look." << endl;
    cout << "The undead form up into ranks and growl a war chant!" << endl;
    cout << commander <<" shouts, CHARGE!!!" << endl;
    cout << endl;

    cout << "Warriors from both sides blitz across the field!" << endl;
    cout << endl;

    cout << "*The Carnage has begun!*" << endl;
    cout << "*Steal, Sparks, and Flesh flies" << endl;
}



void battleOutcome()
{
    int zombieLives = numberOfZombies;
    int humanLives = numberOfHumans;
    int randomNumber = 0;
    int humanDeath = 0;
    int zombieDeath = 0;
    double newHumanLife = 0;
    double newZombieLife = 0;

    Zombies zombieBattleData;
    Humans humanBattleData;

    srand(time(NULL));

    zombieBattleData.setAttackPower(20.0);
    humanBattleData.setAttackPower(35.0);

    zombieBattleData.setDefensePower(15.0);
    humanBattleData.setDefensePower(20.0);

    zombieBattleData.setHealthPoints(150.0);
    humanBattleData.setHealthPoints(300.0);

    while(zombieLives && humanLives > 0)
    {
        randomNumber = 1+(rand()%10);
        if(randomNumber < 6)
        {
            newHumanLife = humanBattleData.healthPoints - zombieBattleData.attackPower;
            if(newHumanLife <= 0)
            {
                humanLives--;
                humanDeath++;
            }
        }else
        {
            newZombieLife = zombieBattleData.healthPoints - humanBattleData.attackPower;
            if(newZombieLife <= 0)
            {
                zombieLives--;
                zombieDeath++;
            }
        }
    }

    if(zombieLives <= 0)
    {
        cout << "Humans have emerged victorious!" << endl;
        cout << "Human Deaths: " << humanDeath << "Zombie Deaths: " << zombieDeath << endl;
    }else if(humanLives <= 0)
    {
        cout << "Zombies have emerges victorious!" << endl;
        cout << "Human Deaths: " << humanDeath << "Zombie Deaths: " << zombieDeath << endl;
    }

我知道代码目前无法正常 运行。我正在做的是测试 运行 确保我没有收到任何错误。我得到的两个错误是:

armySimulatorMain.cpp:25:10: error: 'double ArmyValues::healthPoints' is protected 


armySimulatorMain.cpp:115:67: error: within this context. 

newHumanLife = humanBattleData.healthPoints - zombieBattleData.attackPower;

攻击力和生命力是这样,但是防御力正在清除错误。我不明白为什么他们会被标记。我正在通过 public 函数更改变量,所以不应该允许这样做吗?

另外,我在所有函数之外调用了三个变量,因为它们被多个函数使用。我怎样才能将这些变量插入到我不喜欢的地方,因为它们在一切之上自由浮动?

谢谢你们,我简直不敢相信我忘记了 getters...无论如何代码 运行s 现在非常感谢我一定要记住这次 xD

这不是抱怨你设置值的那一行;如您所说,它使用 public 函数。但是在这里,你尝试读取受保护的成员变量:

newHumanLife = humanBattleData.healthPoints - zombieBattleData.attackPower;

您只尝试读取两个变量,这就是它抱怨的变量。

您需要 public getter 函数来读取值。

您需要执行以下操作:

public:
    double gethealthPoints()
    {
       return healthPoints;
    }

因为 attackPower, defensePower, healthPoints 都是受保护的,所以如果你想访问其中任何一个,你需要 getter,否则你将始终收到保护错误