Help passing around objects members

Hello,
I am working on a final project for class and am having a little trouble. When I create my hero or monster object and assign it values, which is what i was checking with the commented out section in main, I am unable to access that info. For instance in my function to fight where I call the monster.Attack() the name of the warrior or mage object comes up blank. If I run a quick cout everything looks good except for the Hero objects members, like the weapon name etc shows up. Any help would be greatly appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#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);

	/*
	cout << mage.heroType << endl;
	cout << mage.name << endl;
	cout << mage.maxHealth << endl;
	cout << mage.maxMana << endl;
	cout << mage.weapon.damage << endl;
	cout << mage.weapon.name << endl;

	for (int i = 0; i < mage.abilities.size(); i++){
		cout << mage.abilities[i].name << endl;
		cout << mage.abilities[i].type << endl;
		cout << mage.abilities[i].amount << endl;
		cout << mage.abilities[i].manaCost << endl;
	}



	for (int i = 0; i < mage.belt.contents.size(); i++){
		cout << mage.belt.contents[i].type << endl;
		cout << mage.belt.contents[i].amount << endl;
	}
	
	cout << warrior.heroType << endl;
	cout << warrior.name << endl;
	cout << warrior.maxHealth << endl;
	cout << warrior.maxMana << endl;
	cout << warrior.weapon.damage << endl;
	cout << warrior.weapon.name << endl;

	for (int i = 0; i < warrior.belt.contents.size(); i++){
		cout << warrior.belt.contents[i].type << endl;
		cout << warrior.belt.contents[i].amount << endl;
	}
	*/

	do {
		int choice = ShowMenu();

		if (choice == 1){

			Fight(mage, warrior, monster);

		} else if (choice == 2){

			break;

		}
	} while (true);	
		
	cin.ignore();
	cin.get();
	
	return 0;
}
Whats also confusing to me is that the monster.Attack() function properly displays the health decremented for the mage or warrior but wont display on a cout.

Here is the output of the little cout test I did, notice the top 4 and about halfway through the other four referencing the objects herotype, name, maxHealth and maxMana dont register.


-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

Last edited on
1
2
3
4
5
6
7
8
	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;
	}
¿Can you fulfill that request? ¿How can you expect that the stupid compiler can know which name are you refering to?
this->name = name;
You should look up to constructors.


Also, just call srand 1 time. Do it as the first instruction in main.
Got it, I did not get to constructors yet, just glanced at them. I changed all arguments to have a trailing _ and it works. Will change the srand. Thank you!
Topic archived. No new replies allowed.