2 objects interacting with each other

Started by
15 comments, last by immanuelx2 15 years, 10 months ago
How to do it? If i pass an object as an argument thru a function of another object like so
class Player
{
public:
     Player(int hp = 100, int xp = 0, int atk = 17);

     int getHP() const;
     int getXP() const;
     int getATK() const;

     bool attack(Mob& mob);
private:
     int m_HP;
     int m_XP;
     int m_ATK;
};
.. the Mob class definition must come before the Player class definition... However in my Mob class definition i have an argument regarding the Player object...
class Mob
{
public:
     Mob(int hp = 50, string name = "Giant Roach", int attack = 9); //defaults

     int getHP() const;
     string getName() const;
     int getAttack() const;

     bool dmg(int& dmg);
     int attack(Player& player);
private:
     int m_HP;
     string m_Name;
     int m_Attack;
};
how do i get them both to interact if i have to define one before i can use it in the other??
Advertisement
Short answer: use forward declarations, and only include the necessary headers in the .cpp files.

(Also, there are probably other, more abstracted approaches you could take to the design that would involve objects interacting with one another on a higher level, and/or not needing to know the specific type of the objects with which they interacted.)
Quote:Original post by jyk
Short answer: use forward declarations


could u give me an example?
class Mob; // Forward declarationclass Player{public:     Player(int hp = 100, int xp = 0, int atk = 17);     int getHP() const;     int getXP() const;     int getATK() const;     bool attack(Mob& mob);private:     int m_HP;     int m_XP;     int m_ATK;};class Mob{public:     Mob(int hp = 50, string name = "Giant Roach", int attack = 9); //defaults     int getHP() const;     string getName() const;     int getAttack() const;     bool dmg(int& dmg);     int attack(Player& player);private:     int m_HP;     string m_Name;     int m_Attack;};



class Mob;class Player{    bool attack(Mob& mob);    // ...};class Mob{    int attack(Player& player);    // ...};

To define pointers and references you never need to know the contents of the type, just the full name (including namespaces). Only when you dereference or create or destroy such objects must you know the full type specification.
So technically, in the Mob class definition, we don't need to know the full contents of the Player class either. Doesn't hurt if we do, of course ;)
Million-to-one chances occur nine times out of ten!
thanks guys, however i get an error:

Class Mob;class Player{    //...};//...bool Player::attack(Mob& mob){     mob.dmg(m_ATK); //<-------- error main.cpp invalid use of undefined type `struct Mob'      //...}class Mob{    //...};//...
Quote:Original post by immanuelx2
thanks guys, however i get an error:

Class Mob;class Player{    //...};//...bool Player::attack(Mob& mob){     mob.dmg(m_ATK); //<-------- error main.cpp invalid use of undefined type `struct Mob'      //...}class Mob{    //...};//...


[39.11] How can I create two classes that both know about each other?

[39.12] What special considerations are needed when forward declarations are used with member objects?

[39.13] What special considerations are needed when forward declarations are used with inline functions?

Those above links talk about pointers to objects, but the same rules apply for references.
do i just move the

bool Player::attack(Mob& mob){     mob.dmg(m_ATK); //<-------- error main.cpp invalid use of undefined type `struct Mob'      //...}


after the Mob definition?
Quote:Original post by immanuelx2
do i just move the

bool Player::attack(Mob& mob){     mob.dmg(m_ATK); //<-------- error main.cpp invalid use of undefined type `struct Mob'      //...}


after the Mob definition?


Just move the implementation entirely out of the header file into its own source file.
Quote:Original post by fpsgamer
Quote:Original post by immanuelx2
do i just move the

bool Player::attack(Mob& mob){     mob.dmg(m_ATK); //<-------- error main.cpp invalid use of undefined type `struct Mob'      //...}


after the Mob definition?


Just move the implementation entirely out of the header file into its own source file.


alright.

just to recap: I put the Mob and Player class definitions in their own header files (Mob.h, Player.h) and include them in main.cpp using (#include <Mob.h> etc) then include all of the member functions of both classes in main.cpp after the header includes?

This topic is closed to new replies.

Advertisement