Error: global variable is ambiguous (conflict between using namespace STD and global variable)

When you recursively write eight queens, you define a global variable count, which results in the following problem: global variables are not clear.



Finally, I found that in the implementation file.CPP, I used using NAMESPACE STD;
Solutions:
1. Use count instead of ::count (because STD ::count is also in the STD namespace, the compiler is not sure if it is ::count or STD ::count)
Comment out the namespace
3. Or:
using std::cout;
using std::endl;
using std::cin;

Reflection summary:
I. Thinking using NAMESPACE STD
A lot of times it’s not a good idea to use this code (and more importantly: it’s best not to use it in header files)
A good habit is to use STD ::cout and STD ::cin
All the identifiers of the c + + standard library is defined in a namespace called STD, using namespace STD is to import all the system defined identifier, because of the standard library is very large, the programmer when choosing the name of a class or function name is probably has one of the same name, and the standard library that is your own definition of things (variables, methods, classes, etc.) and the system of the identifier name repetition, otherwise there will be a naming conflict error!
C++ definition of global variables
1. It is generally a good idea to declare global variables in a CPP file (if defined in a.h file, multiple layers contain errors that may cause duplication of definitions)
2. Once defined in CPP, you can use the extern keyword in the.h file for an extern declaration (the extern declaration represents the declaration that an externally defined variable is introduced here, rather than another local variable with the same name being declared in this compilation unit)
Then, when other files want to use these variables, just the #include header file is fine, and it won’t cause a double-definition error

3. Comparison of static global variables, global constants (CONST) and ordinary global variables:
1. Static global variables: They can’t be introduced with extern, i.e., extern and static cannot be used together. And static globals are quite different from normal globals.
Static modification of the scope of a global variable is just its own compilation unit (in this compilation unit changes to take effect), when used in other units, the variables have a new memory address, that is, each compilation unit USES it to it opened up a separate space, and make a copy of its original value, so if a unit to modify it, then its value in multiple compilation unit may not be the same

Note:
The static modifier is static, static, static, static, static, static, static, static, static, static, static.
Multiple units contain the static global header file, which doesn’t cause a redefinition error because each unit creates a new space to store it.
2. Global const variable: const like common global variables, global variables to use in a. CPP and initialization, defined in. H header file using extern declaration, then you need to use the place contains. H, its memory address is also different in the multiple compilation unit (this is similar to the static global variable), but because of is constant, not modify the value, so even if the memory address didn’t influence, different values are the same.

Also useless to, refer to: http://blog.csdn.net/jiadebin890724/article/details/40509333

Four, the use of namespaces
To be continued…

Read More: