C + + "string": undeclared identifier

Source: Internet
Author: User

Solution:

To add a using namespace STD; statement after the macro definition.

Explain:

The so-called namespace refers to the various visible ranges of identifiers. All identifiers in the C + + standard library are defined in a namespace called Std.

First, <iostream> and <iostream.h> and format the former has no suffix, in fact, in your compiler include folder you can see that the two are two files, open the file will find that the code inside is not the same. The C + + standard for the header file suffix. h has been explicitly raised and not supported, the earlier implementation defined the standard library functionality in the global space, declared in the header file with the. h suffix, the C + + standard to distinguish it from C, and in order to use the namespace correctly, it is stipulated that the header file does not use a suffix. h. Therefore, when using <iostream.h>, it is equivalent to calling the library function in C, using the global namespace, the early C + + implementation, and when using < Iostream>, the header file does not define a global namespace. Namespace STD must be used so that cout can be used correctly. second, namespace refers to the various visible range of identifiers

All identifiers in the C + + standard library are defined in a namespace called Std. Because of the concept of namespace, there are three choices when using any identifier of the C + + standard library: 1, specifying an identifier directly.

such as Std::ostream rather than ostream. The complete statement is as follows: Std::cout << std::hex << 3.4 << Std::endl; 2. Use the Using keyword.

Using Std::cout; Using Std::endl; Using Std::cin; The above procedures can be written in cout << Std::hex << 3.4 << Endl; 3, the most convenient is to use using namespace std

For example:

#include <iostream>

#include <sstream>

#include <string>

using namespace Std;

All identifiers defined within the namespace Std are valid (exposed). As if they were declared as global variables. Then the above statements can be written as follows: cout << hex << 3.4 << Endl; Because the standard library is very large, the programmer is likely to choose the name of the class or function in the same way as a name in the standard library. So in order to avoid the name conflict caused by this situation, everything in the standard library is placed in the STD of the name space. But this poses a new problem. Countless original C + + code relies on the functionality in the pseudo standard library that has been in use for many years, and they are all under global space. So there are headers such as <iostream> and <iostream.h>, one for compatibility with previous C + + code, and one for supporting new standards. The namespace std encapsulates the name of the standard library, which distinguishes it from the previous header file and generally does not add ". h" to a simple understanding

The C + + language after 98 provides a global namespace namespace, which avoids the problem of global naming conflicts. For an example, please note the following two header files:

One.h

char func (char);

Class String {...};

Somelib.h

Class String {...};

If defined in the way described above, then these two header files cannot be included in the same program because the string class can collide.

The so-called namespace, is a way to encapsulate the name of the library, it is like in the various libraries of a fence.

Related Article

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.