# C++ How to get these classes to communicate?

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

## Recommended Posts

Hey guys, I've got two classes, Game and NetCode and I'm trying to get them able to communicate. Here's some relevant code:
 #include "Game.h" #include "NetCode.h" NetCode netConnection; Game game; main() { game.gameloop(); } etc... 

 class Game{ //DECLARATION OF NETCODE OBJECT? //more stuff } 

 #include "Game.h" Game::gameloop() { here's the main game loop{ //need to be able to call NetCode methods here } } 

 extern Game game; class NetCode { //stuff } 

 #include "Game.h" #include "NetCode.h" //the rest of the NetCode methods. //these need to be able to call Game methods, as shown NetCode::examplefunc() { game.outputText("hey here's some output to parse"); } 

The code I've got compiles, but I now need a way for Game methods to be able to call NetCode methods and vice versa. I tried having a NetCode object as one of Game's member variables, but I couldn't see a way to get NetCode to then call Game's methods. I also have tried giving Game a pointer to a NetCode object as a member variable but have been unable to make that work. Can anyone enlighten me as to the correct way to accomplish this that is in line with OOP principles?

##### Share on other sites
Forward declaration (also, look towards the end of this page (and in case you're really curious, here's more)). Forward declare the classes in the header files, and then include the appropriate header file in the source file to actually use the methods and members of the needed class.

i.e. in Game.h, just do a forward declaration of "class NetCode" and in NetCode.h do a forward declaration of "class Game". Only include the header files in the source files. And bam, you should be good.

##### Share on other sites

Forward declaration (also, look towards the end of this page (and in case you're really curious, here's more)). Forward declare the classes in the header files, and then include the appropriate header file in the source file to actually use the methods and members of the needed class.

i.e. in Game.h, just do a forward declaration of "class NetCode" and in NetCode.h do a forward declaration of "class Game". Only include the header files in the source files. And bam, you should be good.

extern NetCode netConnection;
to Game.h and that let me do
netConnection.packetLoop();
in Game.cpp, so it appears to have worked. It mysteriously crashes when a client connects to the server, but that may not have anything to do with this. Thanks a lot for the help!
Edit: the crash was completely unrelated. Whew. Thanks again.

##### Share on other sites
Why do the two objects need to communicate with each other in two directions?
This usually indicates something wrong in design.

If the two objects (A and B) share same knowledge, the same part should be a third object (C).

Then A, B call C for shared functions.
If A, B want to notify changes, just notify to C, then C dispatch to whoever cared. That's to say, A, B can subscribe to C for any other objects' changing event.

However, if you can't introduce C, then A and B have to hold the counterpart's forwarded pointer or reference.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013549
×