Why is visual studio giving me a bunch of false errors?

Started by
20 comments, last by jpetrie 7 years, 6 months ago

Yeah, that solved about 100 errors, I probably have another circular dependency somewhere though.

Edit :

Found the other circular include, all working now. :D

Glad to hear it.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Advertisement

Oh.. I thought include guards protected against circular dependencies fully.

Not at all, really. Include guards prevent errors related to redefinition or redeclaration of the contents of the included files. Since circular includes also result in redefinition errors, they do protect against that aspect of the problem, but that's not the whole story. They also help protect against infinitely-nested recursive includes, although the preprocessor usually also has an include-depth limit as well.

The issue you're seeing with circular includes, the one include guards cannot protect against, is that you're not ending up redefining anything from your headers, you're ending up with a preprocessed TU that uses things before they are defined. Application.h includes GameState.h which includes Application.h. But header files aren't seen by the compiler. The preprocessed results of .cpp files are. Say some .cpp file #includes Application.h first. The content of Application.h is pasted above the text of that .cpp file, essentially. Then the #include for GameState.h from Application.h is processed and the content of GameState.h is pasted above Application.h. Then the include for Application.h is processed but your include guard causes the preprocessor to early-out. Now you have the file contents in this order, top to bottom:

  • GameState.h
  • Application.h
  • Whatever.cpp

Since you wrote GameState.h such that it required the full definition of Application, the C++ compiler will see that code, not have any idea what Application is (since it's not declared until halfway down the file), and give you an error. That's why you can fix the issue with a forward-declaration.

This topic is closed to new replies.

Advertisement