# Untitled

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

## Recommended Posts

Sorry, but i'm kind of stucked in a simple question, I have a class called theTeam, and thePlayers. /* in theTeam.h */ #include "thePlayers.h" class theTeam { public theTeam(); ~theTeam(); thePlayers * m_players; // all players in the team } /* in thePlayers.h */ class thePlayers { public: thePlayers(); ~thePlayers(); thePlayers getClosestTeammate(); //get the closest teammate frm the team } /* end */ problem here i have is, how do i actually apply the function 'getClosestTeammate()'? since i can't access the data in theTeam from thePlayers class, thru using getClosestTeammate(). Any better solutions?

##### Share on other sites
theTeam* theHoneyBears = new theTeam();theHoneyBears->m_Players->getClosestTeamMate();

Or was there more to your question? I didn't quite understand what you were going for to be honest.

##### Share on other sites
Some possible solutions:

1. Implement getClosestTeammate() in theTeam and pass a player to the function.

2. Add the team as a parameter to the getClosestTeammate() function.

3. When a player is created, tell it what team it belongs to.

4. Store all your teams in a place accessible to all thePlayers and have getClosestTeammate() search through all the teams to find itself. This is probably the least optimum solution in most cases.

5. Pass a pointer to the m_players variable in theTeam to all players when they are created. As long as you don't tell anyone that I told you to do that.

6. Don't have teams or don't have players.

I would suggest one of the first three if possible. It seems to me, without knowing anything about what your doing, the first solution would be the best.

[Edited by - wprintf on June 28, 2008 2:12:03 AM]

##### Share on other sites
Thanks for helping,
i wouldn't try sth like pTeam->m_players->getClosestTeammate(),
because i have too many activities to let players to handle solely,
so it's not ideal to call from pTeam,
and i have other things to let pTeam to handle as well.

the 2nd method u mention is what i'm seekin for,
but how to pass theTeam as a parameter to the GetClosestTeammate() function in thePlayer?
so it only will do recursive loading and will corrupt before running.

what i hope to do is,
i have Move(), Run(), Think() and similiar activities in thePlayer class,
so for e.g, when i Think(),
i hope it can find the closest teammate to him,
but i hope this GetClosestTeammate() to be launched only when it's needed,
so keep updating who's the closestTeammate from theTeam class is not very ideal,
that's what i think.

Any possible solution to this?

##### Share on other sites
The solution is to use forward declarations.
#### theTeam.h #####include "thePlayers.h"class theTeam {public:theTeam();~theTeam();thePlayers * m_players;}#### thePlayers.h #####include "theTeam.h"class theTeam; //forward declarationclass thePlayers {public:thePlayers();~thePlayers();thePlayers* getClosestTeammate(theTeam* team);}

EDIT:
If you wanted to implement getClosestTeammate() in theTeam you could pass theTeam to any function in thePlayers that might need it. Which is what you would have to do anyway to make getClosestTeammate() to work in thePlayers anyway. The only difference is instead of calling getClosestTeammate(team) you would call team->getClosestTeammate(player). Not that either way makes any difference.

##### Share on other sites
ohh forward declaration,

thanks for the help,
i got it worked =)

Deeply appreciated ^^.
God Bless~!

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
13

• 9
• 9
• 23
• 16
• 9
• ### Forum Statistics

• Total Topics
632610
• Total Posts
3007404

×