Header files in C++ (notes)

Any declaration corresponds to a definition, a .h file can have multiple declarations, and a declaration corresponds to a .cpp file (definition).

#include: compile preprocessing directives

.h file: plus the standard header file structure,

Note: When the compiler compiles, it only targets one .cpp file (which is a compilation unit). It does not know the existence of other .cpp files. When they are put together, an error will occur.

 

Wall: means to output all possible warnings (obsessive-compulsive disorder: warnings may be hidden errors)

Header file: You can only put declarations in it, not definitions (if you put definitions, when there are multiple .cpp files in a program, an error will be reported (the error is as noted above ))

(Only the declaration, not the definition) Reason: When multiple .cpps include the same .h, the link encounters duplicate definitions.

 

Sometimes you need to add: #ifndef _X_H_

                  #define _X_H_ (to avoid sometimes repeating the definition) ( conditional compilation, that code does not need to be compiled )

Specification: A header file only puts a .cpp declaration (a .cpp file cannot be included twice in a program, it will be defined repeatedly)


Guess you like

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