C++ 为什么是 std::cout 而不是简单的 cout?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10950083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why std::cout instead of simply cout?
提问by erikbwork
I get these error messages for all cout
and endl
:
我收到所有cout
和以下错误消息endl
:
main.cc:17:5: error: ‘cout' was not declared in this scope
main.cc:17:5: note: suggested alternative:
/usr/include/c++/4.6/iostream:62:18: note: ‘std::cout'
After following the suggestion, everything is fine. Now I am curious, why I had to do that. We used C++ in classes before, but I never had to write a std::
before any of those commands. What might be different on this system?
按照建议后,一切都很好。现在我很好奇,为什么我必须这样做。我们以前在类中使用过 C++,但我从来没有在std::
这些命令之前写过 a 。这个系统可能有什么不同?
回答by FatalError
It seems possible your class may have been using pre-standard C++. An easy way to tell, is to look at your old programs and check, do you see:
您的班级似乎可能一直在使用标准前的 C++。一个简单的判断方法是查看您的旧程序并检查,您是否看到:
#include <iostream.h>
or
或者
#include <iostream>
The former is pre-standard, and you'll be able to just say cout
as opposed to std::cout
without anything additional. You can get the same behavior in standard C++ by adding
前者是预标准的,你可以直接说cout
,而不是std::cout
没有任何额外的东西。您可以通过添加在标准 C++ 中获得相同的行为
using std::cout;
or
或者
using namespace std;
Just one idea, anyway.
无论如何,只有一个想法。
回答by Matthew Walton
In the C++ standard, cout
is defined in the std
namespace, so you need to either say std::cout
or put
在C++标准中,cout
是在std
命名空间中定义的,所以你需要说std::cout
或者放
using namespace std;
in your code in order to get at it.
在您的代码中以获取它。
However, this was not always the case, and in the past cout
was just in the global namespace (or, later on, in both global and std
). I would therefore conclude that your classes used an older C++ compiler.
然而,情况并非总是如此,过去cout
只是在全局命名空间中(或者,稍后在 global 和 中std
)。因此,我认为您的类使用了较旧的 C++ 编译器。
回答by mfontanini
Everything in the Standard Template/Iostream Library resides in namespace std. You've probably used:
标准模板/Iostream 库中的所有内容都位于命名空间 std 中。你可能用过:
using namespace std;
In your classes, and that's why it worked.
在你的课堂上,这就是它奏效的原因。
回答by posttool
You could use the namespace
你可以使用命名空间
But you might offend someone
但你可能会冒犯别人
回答by Hans Z
You probably had using namespace std;
before in your code you did in class. That explicitly tells the precompiler to look for the symbols in std
, which means you don't need to std::
. Though it is good practice to std::cout
instead of cout
so you explicitly invoke std::cout
every time. That way if you are using another library that redefines cout
, you still have the std::cout
behavior instead of some other custom behavior.
你using namespace std;
以前在课堂上做过的代码中可能有过。这明确告诉预编译器在 中查找符号std
,这意味着您不需要std::
. 虽然这是一个很好的做法,std::cout
而不是每次都cout
明确调用std::cout
。这样,如果您使用另一个重新定义的库cout
,您仍然拥有该std::cout
行为,而不是其他一些自定义行为。
回答by Edmund
"std" is a namespace used for STL (Standard Template Library). Please refer to https://en.wikipedia.org/wiki/Namespace#Use_in_common_languages
“std”是用于 STL(标准模板库)的命名空间。请参考https://en.wikipedia.org/wiki/Namespace#Use_in_common_languages
You can either write using namespace std;
before using any stl functions, variables or just insert std::
before them.
您可以using namespace std;
在使用任何 stl 函数、变量之前编写,也可以std::
在它们之前插入。
回答by suman deb
If you are working in ROOT, you do not even have to write #include<iostream>
and using namespace std;
simply start from int filename()
.
如果您在 ROOT 中工作,您甚至不必编写#include<iostream>
,using namespace std;
只需从int filename()
.
This will solve the issue.
这将解决问题。