错误变量受保护
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;
攻击力和生命力就是这种情况,但是,防御力正在清除错误。 我不明白为什么他们被标记。我正在通过公共函数更改变量,所以不应该允许吗?
此外,我在所有函数之外调用三个变量,因为它们被多个函数使用。我怎样才能将这些变量插入我不喜欢的地方,它们自由地漂浮在一切之上?
谢谢大家,我简直不敢相信我忘记了getters...无论如何,代码现在运行非常感谢,我会确保记住这次xD
它不是抱怨你设置值的行;正如你所说,它使用公共函数。但在这里,您尝试读取受保护的成员变量:
newHumanLife = humanBattleData.healthPoints - zombieBattleData.attackPower;
你只尝试读取两个变量,这些变量就是它抱怨的变量。
您需要一个公共 getter 函数来读取值。
你需要做这样的事情:
public:
double gethealthPoints()
{
return healthPoints;
}
因为attackPower, defensePower, healthPoints
都受到保护,所以如果你想访问其中任何一个,你需要一个 getter,否则你将始终收到保护错误
相关文章:
- 如何从其他结构访问受保护的结构变量
- 从模板化父类中的派生内部类访问受保护的成员变量
- C++中的受保护变量
- 如何影响派生类中受保护的基变量
- C++,如何创建线程限制/受保护的变量和函数
- 类的静态成员变量(受保护)的作用域
- 无法在受保护的类中显示变量
- 虚函数在受保护变量上触发编译错误
- 为什么我无法访问作为参数传递到函数中的基类的受保护成员变量?
- 访问受保护的变量 - 继承和子类的复杂情况
- 继承受保护的函数和公共变量C++时发生多重继承编译错误
- 为什么可以从派生实例访问基类中的受保护变量
- 继承中使用的受保护变量
- 有没有办法从类外部声明一个受保护C++变量?
- 访问派生类 C++ 中的受保护变量
- 受保护变量的命名和标准
- 重写派生类中的受保护变量
- 告诉子类对超类的受保护变量执行某些操作是否是一种好的做法(也许是一些已知的设计模式?)?
- 从类C访问类a中的受保护变量,其中C是B的子类,B是a的子类
- 如何访问子节点的受保护变量