C++ : programming redefinition error

C++ programming redefinition error:

 

Error 1: The compiled function implementation has already appeared in the target program .obj file.

Error reason: The code of the implementation part appears in the included header file, or the corresponding .cpp file is included at the end of the header file.

Solution: Strictly separate the declaration and definition (implementation) of the program, store the declaration in the .h and .hpp files, and store the implementation in the .cpp file. Because the .cpp files are compiled separately, if the header file is referenced in multiple places, the implementation part may be repeatedly included in different .cpp files, resulting in multiple compilations.

 

Error 2: Multiple definitions of a variable:

Cause of error: There are variable definitions and declarations in multiple included header files. It should be noted here that variable declarations with memory allocation can also be understood as definitions.

solution:

         Option 1: In other .cpp files, you need to declare the source of the variable, and use the extern keyword to declare. This keyword is only an external declaration of the variable, not a redefinition of the variable. Therefore, there is no memory allocation. It can be understood that there is only one variable in the program. This variable can be called from multiple places, so memory sharing is possible.

         Option 2: Add the static keyword before the variable name of the header file, and define the variable as an internal link, so that each source file containing this header file contains a copy of the internal link variable, and they will not affect each other. This makes this variable a local variable inside the source file.

 

Other notes:

         The class function declaration and implementation of a template class need to be placed in the same header file or in the same source file, but usually in the same header file.

         The default parameter list of the function matches from right to left. If the default parameter list has been initialized when the function is declared, it is not necessary to fill in the default parameter value again when the function is defined; Default parameter values ​​need to be added. It is recommended to add default values ​​in the declaration.

         The Inline keyword is optional, and the compiler will add the function body code to the reference to this function during the program pre-compilation process as needed. Using the principle, the program code segment is small, and the program running time may be shorter than the function call time.

         Control program compilation structure: use precompiled macros, #define, #if defined, #ifdef, #ifdefined(), #ifndef, #else, etc. It is recommended to open the file to determine the macro definition, and take the macro name for each piece of code that needs to be controlled. If you need to modify it later, you only need to modify the initial macro definition, which is convenient for compilation control.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325563241&siteId=291194637