Sign in to follow this  

2 objects interacting with each other

This topic is 3459 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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??

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites


class Mob; // Forward declaration

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;
};

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;
};



Share this post


Link to post
Share on other sites

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 ;)

Share this post


Link to post
Share on other sites
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
{
//...
};

//...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:
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?
No, not quite.

Just read the links posted earlier, and perhaps this article.

Code organization in C++ is not very straightforward, but unfortunately it's something you'll have to come to grips with before you can do anything non-trivial.

Share this post


Link to post
Share on other sites
Quote:
Original post by immanuelx2
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?


Yes that will work.

Though those three links I posted explain all this in better detail and with greater precision.

Share this post


Link to post
Share on other sites
Quote:
Original post by immanuelx2
any idea why #include <mob.h> is not working?

says 'no such file or directory' but there is a mob.h right there in the same /dir as the main.cpp


try #include "mob.h"

Share this post


Link to post
Share on other sites
Quote:
Original post by fpsgamer
Quote:
Original post by immanuelx2
any idea why #include <mob.h> is not working?

says 'no such file or directory' but there is a mob.h right there in the same /dir as the main.cpp


try #include "mob.h"


thank u. that worked. :)

Man this stuff is confusing..... What's the point of header files for these classes if i only have their definition in them and the rest of their functions back in my main.cpp? seems a bit redundant.

Share this post


Link to post
Share on other sites
Quote:
What's the point of header files for these classes if i only have their definition in them and the rest of their functions back in my main.cpp? seems a bit redundant.
Did you read the article I linked to earlier?

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
What's the point of header files for these classes if i only have their definition in them and the rest of their functions back in my main.cpp? seems a bit redundant.
Did you read the article I linked to earlier?


no i just skimmed through it. I will check it out later. thanks!

Share this post


Link to post
Share on other sites

This topic is 3459 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this