# Declaring two classes that know about each other

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

## Recommended Posts

I'm working on my asteroids game, which for my purposes needs a very easy-to-use physics system. Every object that can be affected by it derives from a base class PHYSObject. Up until now, the physics "engine" that I'm making hasn't had any way to keep track of the PHYSObjects that had been created. Now, I came up with a class PHYSRegister that stores a map<IDENTIFIER, PHYSObject *>. In the constructor of a PHYSObject, it registers itself with the register, which gives it an ID number and stored a pointer to it in the map. In the PHYSObject destructor, it sets the value of its identifier in the map to NULL, then proceeds with the rest of the cleanup. Now, this is all well and good, but I haven't yet been able to test it. Both classes need to know about each other - PHYSObject accesses functions of PHYSRegister, and PHYSRegister stores PHYSObjects. I tried this:
class PHYSObject;
class PHYSRegister;

class PHYSObject
{
//definition here
};

class PHYSRegister
{
//definition here
};


but it complains about a "forward declaration" of either PHYSObject or PHYSRegister, depending on what I define first. Same thing when I put them in their own header files. How should I fix this? PS: I tried commenting out all my code, and it compiled a lot faster, but now nothing is displaying! What should I do?

##### Share on other sites
Do you have the actual definitions of the member functions in the same file, or do you merely have the declarations, with the definitions in a separate cpp file? If you have it set up as the former, you'll need to switch to the latter. (Or have the declarations at the top of the header file, and the defintions at the bottom, using inline to get it to compile nicely.)

When you forward declare a class, you can mention that you'll be using pointers or references to that class, but you can't actually make use of that class yet. You can only start calling functions on an object after you have already specified the real declaration.

So with the second model mentioned above, with headers and cpp files, you basically have this:

[Forward Declaration of Classes, so that the compiler knows that a type by this name exists]

[Real Declaration of Classes, describing what members the class contains. You can declare that you intend to make use of pointers and references to any class already fully declared or forward declared, for example, by saying that a parameter of a function is a reference to some forward declared class, or that a member variable is a pointer to some forward declared class.]

[Definition of Classes. Here you can actually make use of any pointers or references to any classes that were forward declared, because you not only have access to the name of the class (from the forward declaration), but you also have access to its actual interface (from the real declaration). This stuff typically goes in the cpp file, which includes all the necessary forward declarations and full declarations at the top.]

##### Share on other sites
And just like that, it works. I love agony :)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634137
• Total Posts
3015757
×