General. H put declaration,. cpp put definition;
The header file should not contain non-inline functions or object definitions. The definition can only appear once in the program, except for the definition of inline functions and const constants. Inline
1. If you do not need the inline keyword, put it in. cpp. If you put the file in. h and the file contains. H, the duplicate definition error already defined in appears.
Cause: each. CPP is compiled into an independent. OBJ; more than two. CPP contains the same. h. h contains the implementation part, which is compiled into two. OBJ. the compiler will see a compilation error if it already exists.
2. If the implementation uses the inline keyword, it should be placed in. h. If you only put it in. cpp, which is equivalent to modifying the external definition with the keyword "inline", the unresolved external symbol error will appear.
Cause: the inline function expands the function body at the call, because the expansion process is during compilation, and because we separately compile the file at the call and class, it is invisible during compilation and calling, so a problem occurs.Inline functions can only appear in the compiled object file.. You must # include in the source code of the call, for example, # include ". cpp". Of course, all. cpp should be modified by inline. Otherwise, the first error will occur.
3. If inline and non-inline functions exist simultaneously,
A. Put inline in. h and put non-inline in. cpp.
B. Place inline in one. cpp, and place non-inline in another. cpp. The reference contains the. cpp file containing inline.
Const global variable
1. Definitions of constants can be placed in header files. Symbolic constants can be defined multiple times in different files of a program (values can be different ).
2. constants can be declared as extern without initialization, so that they can be defined only once in the future. If they are defined multiple times in multiple files, an error is prompted (repeated definition ).
This situation applies when const values cannot be calculated at the compilation time and must be at the runtime.
Constants in the const class
The const constant in the class must be declared as static, because the const must be initialized, but only static Initialization is allowed, and only the sorted type (Integral) can be initialized in the header file, you can also initialize it in a text file.
Other types can only be initialized in text files, but not in header files.
// Example. h
Class Example
{
Public:
Static const int _ size = 20; // OK
Static const double _ rate; // OK can be initialized in another file
Static char name [_ SIZE]; // It is not an ordered type and cannot be initialized in. h.
}
However, in vc6, The _ size in vc6 cannot be assigned with the initial value. Many vc6 instances do not support the standard. It is no problem to compile with GCC.
Template functions can be compiled in two ways,
A. Contains compilation. You can put the function definition in the header file and include it in all the files that instantiate the template function.
B. Compile the function separately, put the function declaration in the header file, and put the definition in the text file.
If only the declaration of the function template is visible in the program text file before the template is instantiated, the standard C ++ requires the user to mark the function template definition as export.
Export template <typename type>
Type min (Type T1, Type T2 ){/*...*/}
Extern
The global declaration that specifies both the extern keyword and an explicit initial value is regarded as the definition of this object, and space is allocated. Subsequent definitions of this object are marked as incorrect.