Solve the problem of mutual inclusion of header files in Visual C ++

Source: Internet
Author: User

When compiling an MFC application, I often encounter the problem that header files contain each other. So I turned to the powerful csdn and got the following answer:

 

Method 1: Use the youyuan class
 
I have two classes in total. Because I want to apply each other in the header files of the two classes, the header files of the other classes are included in the header files of each class, then, declare another class as a friend class in the definition of this class. As follows:
# Include "B. H"
 
Class CA: Public cdialog
{
Friend class CB;
Public:
CB * m_ B; // It must be of the pointer type.
}
 
In another class, you can declare it as follows:
 
# Include "A. H"
Class CB: Public cdialog
{
Friend class CA;

Public:
Ca * M_a; // note that it must be of the pointer type
}

The final key is to create a new class for each other in the constructor of each class!
 
Method 2:

I have two classes in total. Because I want to apply each other in the header files of the two classes, the header files of the other classes are included in the header files of each class, then, declare another class as a friend class in the definition of this class. As follows:
# Include "B. H"
 
Class CA: Public cdialog
{
Friend class CB;
Public:
CB * m_ B; // It must be of the pointer type.
}
 
In another class, you can declare it as follows:
 
Class CA;
Class CB: Public cdialog
{
Public:
Ca * M_a; // note that it must be of the pointer type
}

Include the header file in the CB. cpp File
# Include "ca. H"
 
The final key is to create a new class for each other in the constructor of each class!

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/DavidHsing/archive/2007/11/09/1876788.aspx

 

Common Errors:

Error c2501: 'A': Missing storage-class or type specifiers

Error c2079: 'B' uses undefined class 'B'

 

In addition, there is another article:

Advanced Reference refers to a type that is used to define variables and declare functions before being defined.
In general, C/C ++ requires that all types be defined before use, but in some special cases, such requirements cannot be met, for example, A non-mode dialog box Object Pointer is retained in cmyview, which is used to display/modify some information. To implement the "Apply" button of the dialog box, update the changes made in the dialog box to the View Interface immediately. Therefore, you need to save the View class pointer in the dialog box class, the following code defines a link:
# Ifndef _ myview_h __
# DEFINE _ myview_h __
// This is the header function of the View class.
# Include "mydialog. H"
Class cmyview: Public cview
{
Protected:
Cmydialog * pdlg;
// Other definitions
};
# Endif

# Ifndef _ mydialog_h __
# DEFINE _ mydialog_h __
// This is the definition of the dialog box class
# Include "myview. H"
Class cmydialog: Public cdialog
{
Protected:
Cmyview * pview;
// Other definitions
};
# Endif

From the compiler perspective, compile mydialog. in CPP, the system first defines macro _ mydialog_h __, and then contains myview. h, myview. # include "mydialog. H "since _ mydialog_h _ has been defined, it does not work anymore. In the cmyview class declaration, cmydialog * pdlg; causes the compiler to generate errors such as the "cmydialog" type not defined. The errors in compiling the myview. cpp file can be similar.
Generally, classes A and B need to be referenced from each other, so that one class is first defined, and the other class is then defined, in this way, the class that is first defined after the class is referenced leads to the so-called advance reference.

There are several solutions to errors caused by advanced reference:
1) Use class declaration
Before a class is referenced in advance, a special statement is used to indicate that the identifier is a class name and will be referenced in advance. The usage is as follows:
A) use class classb to declare the class name to be referenced in advance
B) define class classa
C) define class classb;
D) Compile the implementation code of the two classes.
The preceding method applies to all codes in the same file. Generally, classa and classb have their own header files and CPP files respectively.
Methods need to evolve:
A) define the classa and classb respectively, and implement
B) use class classb and class classa at the beginning of the two header files to declare the other
C) The two CPP files contain the header files of the other class.
Note: Do not use a class name to define variable parameters of variables and functions. You can only define references or pointers.
2) Use global variables
Because global variables can avoid advanced references, you do not need to repeat them. My habit is to add the extern statement of the Class Object to the end of the Class header file.
There is no big problem in writing it. The key is not to include it in the header file.
3) use a base class pointer.
This method uses a base class pointer When referencing a class that is referenced in advance. Generally, two referenced classes do not involve their base classes, so
Advanced reference. In the example below, cview * is used in the cmydialog class to replace cmyview *, and cdialog * is used in the cmyview class to replace cmydialog *.
Will not cause advance reference.

Note: In this article, class Aclass is used as the class Aclass declaration to facilitate the description, and class Aclass starts with class Aclass,
The description of member function prototype is called the definition of class, and the part in CPP is called the definition of Class. If you have different understandings of these three words, follow your own intention.
Replace these three words with corresponding words for understanding.

Related Article

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.