1
我正在爲一個類的最終項目工作,我有點麻煩。當我創建我的英雄或怪物對象併爲其賦值時,我正在用main中的註釋部分檢查這些值,但我無法訪問該信息。例如,在我的功能中,打擊我稱之爲怪物的地方。攻擊()戰士或法師對象的名字出現空白。如果我運行一個快速cout,除了像名稱,類型,maxHealth,maxMana這樣的Hero對象成員之外,一切看起來都不錯,但武器名稱等顯示出來。任何幫助將不勝感激!傳遞對象成員
#include <iostream>
#include <vector>
#include <string>
#include <time.h>
#include <windows.h>
using namespace std;
struct Weapon;
struct Belt;
struct Potion;
struct Ability;
class Hero;
class Monster;
void CreateCharacters(Hero&, Hero&, Monster&);
int ShowMenu();
void Fight(Hero&, Hero&, Monster&);
struct Weapon{
std::string name;
int damage;
};
struct Belt{
std::vector<Potion> contents;
};
struct Potion{
enum e_Type{Health, Mana} type;
int amount;
};
struct Ability{
std::string name;
enum e_Type{Offensive, Defensive, Neutral} type;
int manaCost;
int amount;
};
class Hero{
public:
std::string name;
int maxHealth;
int maxMana;
public:
Weapon weapon;
Belt belt;
std::vector<Ability> abilities;
enum e_HeroType{Warrior, Mage} heroType;
int health;
int mana;
void CreateHero(std::string name, int maxHealth, int maxMana, Hero::e_HeroType heroType){
name = name;
maxHealth = maxHealth;
maxMana = maxMana;
health = maxHealth;
mana = maxMana;
heroType = heroType;
}
void CreateWeapon(std::string name, int damage){
weapon.name = name;
weapon.damage = damage;
}
void FillBelt(Potion::e_Type type, int amount){
Potion potion;
potion.type = type;
potion.amount = amount;
for (int i = 0; i < 3; i++){
belt.contents.push_back(potion);
}
}
void CreateAbility(std::string name, Ability::e_Type type, int manaCost, int amount){
Ability ability;
ability.name = name;
ability.type = type;
ability.manaCost = manaCost;
ability.amount = amount;
abilities.push_back(ability);
}
void Attack(){}
void TakeDamage(){}
void UseItem(){}
};
class Monster{
private:
std::string name;
int maxHealth;
public:
int health;
Weapon weapon;
void CreateMonster(std::string name, int maxHealth){
name = name;
maxHealth = maxHealth;
health = maxHealth;
}
void CreateWeapon(std::string name, int damage){
weapon.name = name;
weapon.damage = damage;
}
void Attack(Hero &mage, Hero &warrior, Monster &monster){
std::cout << "The troll takes a random swing!" << endl;
Sleep(3000);
srand(time(NULL));
int randomNumber = rand() % 2 + 1;
if (randomNumber == 1){
mage.health -= monster.weapon.damage;
cout << mage.name << " has been struck!" << endl;
Sleep(2500);
cout << "The health of " << mage.name << " has dropped to " << mage.health << endl;
} else if (randomNumber == 2){
warrior.health -= monster.weapon.damage;
cout << warrior.name << " has been struck!" << endl;
Sleep(2500);
cout << "The health of " << warrior.name << " has dropped to " << warrior.health << endl;
}
}
void TakeDamage(){}
};
void CreateCharacters(Hero &mage, Hero &warrior, Monster &monster){
mage.CreateHero("Gandalf", 80, 100, Hero::Mage);
mage.CreateWeapon("Rod of Ages", 5);
mage.FillBelt(Potion::Mana, 25);
mage.CreateAbility("Fireball", Ability::Offensive, 15, 25);
mage.CreateAbility("Heal", Ability::Defensive, 10, 25);
warrior.CreateHero("Spartacus", 100, 0, Hero::Warrior);
warrior.CreateWeapon("The Destroyer", 35);
warrior.FillBelt(Potion::Health, 25);
monster.CreateMonster("Trollio", 250);
monster.CreateWeapon("Mighty Club", 15);
}
int ShowMenu(){
int choice;
cout << "\nWelcome to the Battle!" << endl;
cout << "----------------------" << endl;
cout << "Please make a choice from the following:" << endl;
cout << "1. Fight Now!" << endl;
cout << "2. Quit" << endl;
cin >> choice;
return choice;
}
void Fight(Hero &mage, Hero &warrior, Monster &monster){
std::cout << "\nYou have chosen to fight!" << endl;
Sleep(3000);
monster.Attack(mage, warrior, monster);
}
int main(){
Hero mage;
Hero warrior;
Monster monster;
CreateCharacters(mage, warrior, monster);
do {
int choice = ShowMenu();
if (choice == 1){
Fight(mage, warrior, monster);
} else if (choice == 2){
break;
}
} while (true);
cin.ignore();
cin.get();
return 0;
}
請告訴我也困惑,我就是monster.Attack()函數可以正確地顯示爲減少對COUT法師和武士,但不會顯示健康。
這裏是小COUT測試我做的輸出,注意頂部4和大約一半的其他四個引用herotype,名稱,maxHealth和maxMana不註冊的對象。
-858993460
-858993460
-858993460
5
Rod of Ages
Fireball
0
25
15
Heal
1
25
10
1
25
1
25
1
25
-858993460
-858993460
-858993460
35
The Destroyer
0
25
0
25
0
25
是!非常感謝。不能相信我不能upvote還是哈哈。 – Randallsm83
@ Zer0s0phT我以你的名義向上投票。 ;)避免這個問題的一個常見習慣是用尾部下劃線來命名你的成員變量,例如,名稱_。在一些微軟世界的代碼中,你也可以看到m_作爲成員變量的前綴。 –
明白了!也感謝你的投票。 – Randallsm83