Today, when I wrote Qt, I encountered two classes of mutual problems, Class A To use Class B, class B to use the Class A.
Class A:a.h
#ifndef a_h #define A_h<b.h>class a{public: A ();}; #endif // a_h
A.cpp
" a.h " a::a () { b b;}
Class B:b.h
#ifndef B_h #define B_h<a.h>class b{public: B ();}; #endif // B_h
B.cpp
" B.h " b::b () { a A;}
It is problematic to compile as described above.
For A.cpp, the A.H is included, so the A.cpp is expanded first, as follows
#ifndef a_h #define A_h<b.h>class a{public: A ();}; #endif // a_h a::a () { b b;}
In fact, it can not be seen, so continue to #include <b.h> , expand the need to add b.cpp code, as follows
#ifndef a_h#defineA_h#ifndef B_h#defineB_h#include<a.h>//for this sentence, because the second line defines the a_h, it will not be expanded, so this sentence can be removed. classb{ Public: B ();};#endif //B_hb::b () {a A;}classa{ Public: A ();};#endif //a_ha::a () {b b;}
Look at the code above to find that the A in class B's constructor precedes the declaration of Class A, so the problem arises, and class A is undefined.
So the solution is to add a pre-declaration, either directly in front of the header file or directly followed by the declaration of this class, for the above example, the solution is to add the class header file containing the preceding declaration of the class, as follows:
A.h
#ifndef a_h #define A_hclass<b.h>//class B; class a{public: A ();}; #endif // a_h
D..
#ifndef B_h #define B_hclassa <a.h>//Class A; class b{public: B ();}; #endif // B_h
how QT C + + object header files are included with each other