错误变量受保护

Error Variable is Protected

本文关键字:受保护 变量 错误      更新时间:2023-10-16
    #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;

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

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

谢谢大家,我简直不敢相信我忘记了getters...无论如何,代码现在运行非常感谢,我会确保记住这次xD

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

newHumanLife = humanBattleData.healthPoints - zombieBattleData.attackPower;

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

您需要一个公共 getter 函数来读取值。

你需要做这样的事情:

public:
    double gethealthPoints()
    {
       return healthPoints;
    }

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