错误变量受保护

时间:2016-01-06 02:39:51

标签: c++ 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;

这是攻击力和健康能力的情况,然而,防御能力正在清除错误。我不明白为什么他们会被标记。我通过公共功能更改变量,所以不允许这样做?

另外,我在所有函数之外调用三个变量,因为它们被多个函数使用。如何将这些变量插入某个我不喜欢的地方,以至于它们在所有东西上方自由浮动?

谢谢你们,我无法相信我忘记了获取者......无论如何,代码运行现在非常感谢我一定会记得这次xD

2 个答案:

答案 0 :(得分:1)

没有抱怨你设置值的行;正如你所说,它使用公共功能。但是在这里,您尝试读取受保护的成员变量:

newHumanLife = humanBattleData.healthPoints - zombieBattleData.attackPower;

你只会尝试阅读两个变量,而这些变量就是它所抱怨的。

您需要使用公共getter函数来读取值。

答案 1 :(得分:0)

您需要执行以下操作:

public:
    double gethealthPoints()
    {
       return healthPoints;
    }

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