I had a problem with using GetOwner() function - VS underlined it with red even if it worked and there was unclear error text when I was placing a cursor on it. After reading answers to this and this questions, I resoled this problem by including Actor.h in my .cpp file. But when I am trying to include it in .h everything breakes down and at the cost of one unclear red underlining I get many other of them, including some macroses. That drives me crazy because, as I know, #include is just a command to preprocessor to copy the content from specified file to the place, where this #include was done. Considering that in header file i have only the declaration of a class, the only difference that should be between including in the top of the .cpp and in the top of .h is the visibility of the included header for class declaration, so how additional include can break the program? Which headers can I include in .h without breaking a program and why I can not include all headers that I need?
I read IWYU Reference Guide but it says “All header files include their required dependencies.” and “*.cpp files include their matching *.h files first.” and I have a questions about both of these statements:
- As I understand, I must include all required dependencies in header - how will I do this if it creates errors?
- The fact that “*.cpp files include their matching *.h files first.” means that matching *.h should be the only one include in this file? If no - then should I include there all required headers, including those that are already included in matching .h?
- You should use as few includes as you can, because more includes mean longer build times, if you want, you may google for “c++ forward declaration”, these are even more helpful for fast builds.
- Include only where it is needed, if you use something only in the cpp, only include the needed headers in the cpp
- Includes “should” not break your build.
- If your code is working and only IntelliSense is underlining code red, you don’t have to include anything. These are not errors but information. Often you don’t get the list of members or something, but if you don’t need it, don’t waste your time.
About your questions:
- As I understand, I must include all
required dependencies in header - how
will I do this if it creates errors?
Then something is not right. If you must include something, then you must include it and if that creates an error you have to fix the problem. Sorry to not be able to really help here but that is just the way it is.
- The fact that “.cpp files include
their matching .h files first.” means
that matching *.h should be the only
one include in this file? If no - then
should I include there all required
headers, including those that are
already included in matching .h?
This means there can be as many includes as you need/want,
but the include of the header which belongs to this .cpp file should be the first include.
This is not always correct (precompiled headers…) but you can use it as a rule for now.
So, just to be clear, if I include “ModuleManager.h” in .h, do I need to include “ModuleManager.h” in the .cpp or in this case there is no need?
(Knowing that I will use this dependency in .cpp.)