how QT C + + object header files are included with each other

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.