Advertisement

Include order not working (c++)

Started by June 05, 2018 08:47 AM
13 comments, last by ChaosEngine 6 years, 3 months ago

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.

 

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)

 

Advertisement

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!

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).

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement