The so-called leading reference refers to a type that is used to define variables and declare functions before it is defined.
Generally, C/C++ requires that all types must be defined before use, but in some special cases, this requirement cannot be met. For example, a non-mode dialog object pointer is retained in the CMyView class, which is used to display/modify some information. In order to implement the "Apply" button of the dialog box, the modifications made by the dialog box are immediately updated to the view interface. To do this, you need to save the pointer of the view class in the dialog box class, so that the definition relationship becomes the following code:
#ifndef __MYVIEW_H__
#define __MYVIEW_H__
//This is the header function of the view class
#include ""
class CMyView::public CView
{
protected:
CMyDialog * pDlg;
// Here are other definitions
};
#endif
#ifndef __MYDIALOG_H__
#define __MYDIALOG_H__
//This is the definition of the dialog box class
#include ""
class CMyDialog::public CDialog
{
protected:
CMyView * pView;
//Other definitions
};
#endif
From a compiler perspective, when compiling, the system first defines the macro __MYDIALOG_H__, and then contains the #include "" Since __MYDIALOG_H__ has been defined, it no longer works. In the declaration of the CMyView class, CMyDialog* pDlg ; will cause the compiler to generate errors such as "CMyDialog" type not defined, and errors occurring in the compilation file can be obtained similarly.
In general, class A and class B need to refer to each other, so one class will inevitably be defined first and another class will be defined later. This leads to the so-called advanced reference when the class that is defined first is referenced and then the class that is defined first is referred to.
There are several ways to deal with errors caused by advanced references:
1) Use class declaration
Before referring to a class ahead of time, first use a special statement to indicate that the identifier is a class name and will be referenced ahead of time. Its usage is:
a) Use class ClassB; declare the class name that will be referenced in advance
b) Define class ClassA
c) Define class ClassB;
d) Prepare implementation code for two classes.
The above method is suitable for all codes in the same file. Generally speaking, ClassA and ClassB have their own header files and cpp files respectively.
The method needs to evolve into:
a) Define ClassA and ClassB respectively, and implement it in the cpp file
b) Declare the other party with class ClassB; and class ClassA; at the beginning of the two header files respectively
c) The header file containing another class in the two cpp files
NOTE: This method must remember that you cannot use class names to define variables and functions' variable parameters, but can only be used to define references or pointers.
2) Use global variables
Since global variables can avoid pre-references, there is no need to repeat them. My habit is to add the extern statement of the class object to the end of the header file of the class. There is no big problem with how you like to write it. The key is to ensure that you do not include it randomly in the header file.
3) Use base class pointers.
This method uses base class pointers when referring to the pre-referenced class. Generally speaking, two classes that refer to each other do not involve their base class, so they will not cause advanced references. Take the example to start: use CView* instead of CMyView* in the CMyDialog class, and use CDialog* instead of CMyDialog* in the CMyView class, so that it will definitely not cause advanced references.
illustrate:In this article, for the sake of convenience of narration, the class AClass; statement is called the declaration of class AClass class member variables, member function prototypes, etc. starting with class AClass is called the definition of the class, and the part in the CPP is called the definition of the class. If you have different understandings of these three words, please change these three words into corresponding words according to your original intention.
ps: I used the first method to solve the problem.