Class Issue

I'm fairly new to C++, so to learn a bit through practice, I decided to make a simple little RPG-like turn based battle system. This isn't the exact code, but its bloated enough as is and this conveys the problem I'm having.

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
#include <iostream>
#include <string>
using namespace std;

// player classes

class monk_fighter {
	
	public:

	int health;
	int mana;

	int strength;
	int agility;
	int intelligence;

	int weaponDamage;


	monk_fighter(int a, int b);

	void attack(int &a) {
		int damage;

		damage = strength + weaponDamage ;
		a = a - damage ;

	};


	void defend();

	void ability() {
	health = health + 10;
	}

};


// Enemy Classes
//


class Enemy
{
public:
    int health;
    int weaponDamage;

	int strength;
	int agility;
	int intelligence;

	int ExperienceWorth;

    void Enemy::attack(int &a) {
		int damage;
		cout << "The enemy strikes!" << endl;
		damage = strength; + weaponDamage ;
		a = a - damage ;

	}



};

class SkeletonEnemy : public Enemy
{
public:
   
	SkeletonEnemy(int a, int b);

};


// constructors

monk_fighter::monk_fighter(int a, int b) {   //stats all placeholders for now
	
	health = a;
	mana = b;

	strength = 5;
	agility = 5;
	intelligence = 5;

	}

SkeletonEnemy::SkeletonEnemy(int a, int b) {   
	
	health = a;

	strength = b;
	agility = 5;
	intelligence = 5;

	}

bool P1Turn = 1;
	bool P2Turn = 0;

	bool GameActive = 1;
	int queryAnswer;


monk_fighter Player1(10, 10);

//
//
//   some mechanism for choosing a random enemy class
//

int main(void)
{

	bool P1Turn = 1;
	bool P2Turn = 0;

	bool GameActive = 1;
	int queryAnswer;


while(GameActive == true){

	while(P1Turn == true){

		if(Player1.health <= 0){
			GameActive = false;
			P1Turn = false;
			P2Turn = false;
			break;
		}

		cout << "What action does the monk take?" << endl << "1: Attack" << endl << "2. Defend" << endl << "3. Use ability" << endl << endl;

		cin >> queryAnswer;

		switch(queryAnswer){
		case 1:
			Player1.attack(Enemy.health);

			P1Turn = false;
			P2Turn = true;

			break;
		case 2:
			//placeholder
			P1Turn = false;
			P2Turn = true;

			break;
		case 3:
			Player1.ability();

			P1Turn = false;
			P2Turn = true;

			break;
			}
		}

	while(P2Turn == true){

		if(Enemy.health <= 0){
			GameActive = false;
			P1Turn = false;
			P2Turn = false;
			break;
		}


		cin >> queryAnswer;

		switch(queryAnswer){
		case 1:
			Enemy.attack(Player1.health);

			P2Turn = false;
			P1Turn = true;

			break;
		case 2:
			//placeholder

			P1Turn = true;
			P2Turn = false;

			break;
		case 3:
			Enemy.ability();

			P1Turn = true;
			P2Turn = false;

			break;
			}
		}
	
}


	return 0;
}


I want to have be able to randomly choose a subtype of the Enemy class, which I can do, but my problem is that I have no idea how to reference it once I do in the actual main loop. I can't do "Player1.attack(Enemy.health);" (I had it set up this way while testing the rest of the program, using a constructed Enemy1 or however its properly said) since it wants an actual instance of the class.

How do I get around this?
Instead of void attack(int &a) where you are passing an integer, you need to pass a class instance. Like this : void attack(Enemy &).

Then, when attacking, you can use something like this: Player1.attack(enemy object);.

At line 142, you are trying to attack the Enemy class, instead of attacking an instance of that class. You need to create an instance of Enemy class:
Enemy bandit.


Therefore you can say:
Player1.attack(bandit);.
Last edited on
Topic archived. No new replies allowed.