RPG: battles and passing references

for the past week or two I've been rebuilding a small rpg that i wrote in visual basic up in c++, coming across some issues such as inventory and others that thankfully i've been able to pull through, however i see an issue that will arise soon when i come to recreate my battle system due to the way c++ requires prior notice of classes when calling methods between them.

My, hero class atm was created in a seperate function contained within my system class (just a large container to store everything) which is then passed by reference into my main menu
1
2
3
4
5
6
7
8
9
10
11
12
 //creates the player object with the code defined for an arcanist
			   Player *hero;
				   hero = new Arcanist;
                                    F_menu(hero);


void F_menu(Player *character)
	{
//menu stuff

};


Our arcanist being an inherited job class and so on, From here i was hoping to pass our Hero along the functions where needed until we reach our battle function, were by a similar method to our hero creation, we would create the enemy we wanted and then pass them both into the battle function

1
2
3
4
5
6
7
8
9
//Example:

void F_enemy_creation (Player *character) {

monster *enemy;
enemy = new (type of monster);
F_battle(character, enemy);

}


Here is were i can see our issue arising, both our hero and monster is now stored in our battle function for the moment, then by following a turn based theme, our function would call our players battle method ( with reference to our monster) and the our monsters battle method ( with reference to our layer)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//example:

void F_battle(Player *character , monster *enemy)
{

our loop {

character-> player_turn(enemy);
enemy-> monster_turn(character);



//until either dies
}
//end battle stuff


}


due to as i previously said above and you all well know, c++ will throw a wobbly here as I have written out all our classes above, (.h and .cpp files didnt like me) therefore depending on which class is declared above the other, one of them will throw an unknown identifier error.

potentially i could write a third man function in between them which takes the input from one and outputs it to the other but to me, it seems like it would end up being more of a hassle when attempting to pass over things like status effects, our ai for the monsters that would select an ability depending on what our remaining health is.

overall what i'm asking help for in is that is there a way to break away the top
down reading of c++ to allow classes to identify with those, below and above,
than you for your help
You should put your post on the 'general c++ programming' page. You will get more answers than if you stay on the 'beginner' page.
should probably as it's pushing past the beginner but i've only just started learning c++ over the past couple of weeks so i didn't necessarily want to step out of what i'm not
You can declare the existence of a class without having to define it all.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Player; // declared but not defined

class Monster
{
   void eatPlayer(Player theVictim); // Fine. I know Player is a class 
};

class Player  // Now defined
{
 public:
  void eatMonster(Monster theVictim);  // Fine. Monster class already defined
  int someNumber;
};


void Monster::eatPlayer(Player theVictim)
{
  theVictim.someNumber; // access object with no problems as it is now fully defined
}

So thats where i was going wrong, initially i was writing out methods for classes like what you written there, monster::function, however i found that things where getting a bit messy so i began to move all the functions within the classes

class player {

void dosomething(){
// yadda yadda
}


}

however i take it that as long as i define the member functions outside, and below the monster class, it will work
Topic archived. No new replies allowed.