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.
#include <iostream>
#include <string>
usingnamespace 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.
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.