Error: The global variable is not clear (the using namespace std conflicts with the global variable), namespacestd

Source: Internet
Author: User

Error: The global variable is not clear (the using namespace std conflicts with the global variable), namespacestd

When writing the eight queens recursively, a global variable count is defined. The following error occurs:Unknown global variables.


Finally, I found that in the implementation file. cpp, I used using namespace std;

Solution:

1. replace count with: count (because std: count is also in the std namespace, the compiler is not sure whether it is: count or std: count, so it is not clear)

2. comment out the namespace

3. Alternatively, use:

Using std: cout;
Using std: endl;
Using std: cin;


Summary:

I. Thinking about using namespace std

Most of the time, using this code is not a good method (more importantly, it is best not to use it in header files)

A good habit is to use std: cout and std: cin.

All identifiers in the C ++ standard library are defined in a namespace named std. using namespace std imports all the system-defined identifiers because the standard library is very large, when selecting the class name or function name, the programmer may have the same name as a name in the standard library, that is, what you define (variables, methods, classes, etc) it cannot be the same as the identifiers in the system. Otherwise, a name conflict error occurs!

Ii. Definitions of global variables in c ++

1. It is generally best to declare the global variables in the cpp file (if defined in the. h file, multi-level inclusion may cause repeated definitions)

2. after being defined in cpp, you can. use the extern keyword to declare in the hfile (the extern declaration indicates the declaration of introducing external definition variables here, rather than declaring a local variable with the same name in the current compilation unit)

When other files need to use these variables, as long as # include thisHeader fileAnd will not cause repeated definition errors.


3. Comparison of static global variables, global constants (const), and common global variables:

1. static global variables: global variables modified using static. They cannot be declared using extern, that is, extern and static cannot be used together; static global variables are very different from common global variables.

The scope of the static modified global variable is only the compilation unit in which it is located (the change takes effect within the current compilation unit). When used in other units, the variable has a new memory address, that is to say, each compilation unit that uses it opens up a separate space for it, and copies its initial value so that if a unit modifies it, the values of multiple compilation units may be different.

Note:

The static global variable declaration and definition are integrated. static global variables are also defined in the header file, unlike common global variables;

Multiple compilation units contain the header file where the static global variable is located, which does not cause redefinition errors, because each compilation unit opens up a new space to store it;

2. const global variables: the const global variables are used in the same way as common global variables. define and assign the initial values in cpp. h. Use extern in the header file, and then include the following content. h. The memory address in multiple compilation units is also different (this is similar to the static global variable). However, the value cannot be modified because it is a constant, so even if the memory address is different, the value is the same.


Still useless, reference: http://blog.csdn.net/jiadebin890724/article/details/40509333


Iv. namespace usage

To be continued .....


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.