1. What do you mean by repeating definition
When we write large projects, there are many files and complex relationships. Problems that often cause the header file to be repeatedly defined.
What do you mean by repeating the header file definition?
To give the simplest example:
There is a header file "c.h" that has two references to the "c.h" header file in "A.h". In "A.H" there are the following statements:
<span style= "FONT-SIZE:18PX;" > #include "c.h" #include "c.h" </span>
There are two possible reasons for this childish mistake, and the first is that you will "ch.h" less by accidentally shaking your hand. Or are you just curious to write two of them. The second is the least noticeable, that is, repeated calls to "c.h" in other header files. Engineering calls intricate, accidentally called two times is very easy to happen.
As in the header file for "B.h", there are:
#include "c.h"
"A.h" also refers to "c.h" and "B.h" which are:
#include "b.h" #include "c.h"
2. How to resolve duplicate definitions
One good way to resolve the first one is to add #ifndef #define before each header file ... #endif的宏定义. As the header file "C.h" is written in the following form.
#ifndef c_h#define c_h/*c.h all content,! Note that this is all content */#endif </span>
3. Why can I solve the problem of duplicate definitions? Why does this solve the problem of repeated references?
For example, the second case above, "a.h" first refers to "B.h", "B.h" in the call "C.h". The first time there is no macro defined for C_h, the reference "C.h" succeeds. When you refer to "C.h" for the second time, "C.h" will no longer be referenced because C_H is already defined. This is a good solution to the problem of repeated references.
! Note 1 have a habit. Each header file is added with such a macro definition.
! Note 2 macros define a # define C_H the contents below are all the contents of the original "C.h".
Thinking about the repeating definition of C + + header file