Jump to content
  • Advertisement
suliman

Include order not working (c++)

Recommended Posts

It's not just about those 2 files, it's about any files they include. If A.h includes B.h, B.h includes C.h, and C.h includes A.h, that's circular, even though no 2 files directly include each other.

Probably the best way to fix this is to do what Bregma said, refactor your headers so that each one has only 1 class in it, and #includes as few other headers as possible. This makes it easier to only include what you need, and easier to go through them and look for cycles. Those cycles can be fixed with forward declarations and pointers or other ways of referencing that don't require the circular dependency.

 

Share this post


Link to post
Share on other sites
Advertisement

Another way to find the problem is to draw a dependency tree. Each include file is a square on paper. From each square, draw an arrow to other squares it directly includes (ie there is an #include line in the file at the back of the arrow).

You should not be able to follow the arrows and end up in a square where you have already been (ie cycles are not allowed)

 

Share this post


Link to post
Share on other sites

Yeah there must be something like that going on. I started picking apart this very old project but it become to big of an effort so i just declared them in global scope. Not so pretty but it works well.

I will certainly avoid this in new projects I start:)

Thanks!

Share this post


Link to post
Share on other sites
On 6/6/2018 at 2:28 AM, suliman said:

I started picking apart this very old project but it become to big of an effort so i just declared them in global scope. Not so pretty but it works well.

I wouldn't do that unless you absolutely have to (deadlines, etc). 

This is a really good opportunity to pick this apart and learn how to restructure your code. It's a very useful skill to have because you will almost certainly encounter code like this again (and probably written by someone else).

Share this post


Link to post
Share on other sites

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

  • 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!