简体   繁体   English

关于C ++中的std:cout

[英]About std:cout in C++

Is there an error in this code: 这段代码中是否有错误:

#include <iostream>
using namespace std;

int main()
{
    std:cout << "hello" "\n";
}

GCC detects no error but std:cout does not seem standard. GCC检测到没有错误但是std:cout似乎不是标准的。

There's no error. 没有错误。 I could rewrite your code to make it clearer: 我可以重写你的代码,使其更清晰:

#include <iostream>
using namespace std;

int main()
{
std:
    cout << "hello" "\n";
}

You created a label named std . 您创建了一个名为std的标签。 cout is used unqualified, which is okay since you have the using-directive for std above it. cout使用不合格,这是可以的,因为你有std上面的using-directive And you can concatenate string literals by writing them next to each other as you did. 并且您可以通过将字符串文字彼此相邻地连接起来来连接字符串文字。 This is perfectly well-formed code that prints "hello" followed by a newline. 这是完美格式的代码,打印“hello”后跟换行符。

You're defining a label std and then you're calling cout . 你正在定义一个标签std然后你正在呼叫cout This is legal because you have using namespace std; 这是合法的,因为你using namespace std;

The code has an issue. 代码有问题。 while trying to instruct the compiler to use the namespace std, we are trying to call the function cout which is defined in the scope of std. 在尝试指示编译器使用命名空间std时,我们试图调用在std范围内定义的函数cout。

thus the correct use of the scope resolution operator is 因此,正确使用范围解析运算符是

    'std::cout '

and not 并不是

    std:cout.

And others have pointed out by writing 其他人通过写作指出

    std:

what you do is create a label. 你要做的是创建一个标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM