Circular dependencies with headers. Using #ifndef and #define

后端 未结 3 1654
后悔当初
后悔当初 2021-01-16 03:55

I have the very simple following code:

main.cpp

#include \"ui_library_browser.h\"
#include 
#include \"StartWindow.h\"
int          


        
3条回答
  •  孤独总比滥情好
    2021-01-16 04:43

    So called "header guards" are used to prevent a bit different kind of error: including same header multiple time through different indirect inclusions in one compile unit. For example, you include "a.h" from main.cpp and then include "b.h" from main.cpp, that includes "a.h" itself somewhere inside.

    In your case two headers try to include each other circurally, that is not possible - C/C++ preprocessor works as simple text "copy-paste" and this case would invent infinite recursion of text insertion.

    And I really don't see why would you need "StartWindow.h" inclusion in "MainWindow.h" header.

提交回复
热议问题