Jump to content
  • Advertisement
Sign in to follow this  
lorrain

Circular Dependencies

This topic is 4240 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

Hi, I'm getting mind blowing errors that I assume have to do with circular dependencies, and I'm wondering if there's a smarter way to do this: Currently, I have a CMap class that contains all the entities on that map (separated by category, so one list of CCharacters, and one of CWorldItems.) Then, CCharacter and CWorldItem are both derived from CObject, which has a pointer to the parent CMap. I need the pointer to CMap, so if CCharacter says something, it can pull a list of characters from CMap. Is this good, or can you recommend a better way? Because it's very flaky, I can get errors like: "... undefined 'struct CFoo'", "expected class-name before '{' token", "... forwarded declaration...", etc. Theses errors can take awhile to troubleshoot, and/or have me eventually rewriting how things work.

Share this post


Link to post
Share on other sites
Advertisement
Instead of having the pointer to CMap and passing that around - have an external list that CMap can read from and CCharacters can read and/or update to - this way, the CCharacters object won't need a direct reference to the CMap object...

just a thought..

Share this post


Link to post
Share on other sites
Agree, parent-child references are a good (and common) way to do this.

For your undefined struct problem, you'll need to forward-declare your structs & classes to make the compiler happy:

// #include "CMap.h"
class CMap;

class CCharacter {
CMap* m_map;
....
};

In this fragment, I comment out the include to the full definition of CMap from my CCharacter.h, because of the circular reference problem you mention. Since the member is a pointer, all the compiler needs to know is that some class called CMap exists. It doesn't need to know its internals since the size of the pointer is the same. You could also use a void*, but this is type-safe.

The only downside, is that you can't have any inline methods in CCharacter.h that try to dereference m_map.

Hope this helps.

*edit*

As an aside, the less headers you can avoid #including, the faster your compile time will be.

Share this post


Link to post
Share on other sites
http://www.gamedev.net/community/forums/topic.asp?topic_id=195257&whichpage=1�

http://www.gamedev.net/reference/programming/features/orgfiles/page3.asp

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!