Analysis of the reason for g++ compilation error report "was not declared in this scope"

When compiling the program, the prompt: "was not declared in this scope".
After analysis, it is found that the reasons are as follows:
1. Variables, functions, or classes are not declared or defined. This is the simplest case~ but it is the mistake I often make (basically the most likely among these reasons).


In addition, someone on the Internet pointed out that the following reasons can also cause this prompt error:

2. When header files #include each other, a dependency error is caused. For example, header files form a circular dependency,

/***file a ****/
#ifndef FILE_A_
#define FILE_A_
#include <file b>
#endif 


/****file b ***/
#ifndef FILE_B_
#define FILE_B_
#include <file a>
#endif 

If the variables, functions and classes in file a are used in file b, due to the effect of #ifndef and #define, the #include <file a> statement in file b cannot be executed when the #include <file a> statement in file a is executed. b> cause an error.


3. When the header file was named, it accidentally had the same name as the header file in the library, and the header file of the library was used in the program.
In this way, the macro names are duplicated, and there must be a header file that loses its effect because of #ifndef. Naturally, other files cannot find the declaration in this header file, and it prompts was not decleared in this scope.


Guess you like

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