简体   繁体   English

为什么是 std::cout 而不是简单的 cout?

[英]Why std::cout instead of simply cout?

I get these error messages for all cout and endl :我收到所有coutendl错误消息:

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.我们以前在类中使用过 C++,但我从来不需要在任何这些命令之前编写std:: What might be different on this system?这个系统可能有什么不同?

It seems possible your class may have been using pre-standard C++.您的班级似乎可能一直在使用标准前的 C++。 An easy way to tell, is to look at your old programs and check, do you see:一个简单的判断方法是查看您的旧程序并检查,您是否看到:

#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.前者是预标准的,你可以只说cout而不是std::cout而没有任何额外的东西。 You can get the same behavior in standard C++ by adding您可以通过添加在标准 C++ 中获得相同的行为

using std::cout;

or要么

using namespace std;

Just one idea, anyway.无论如何,只有一个想法。

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或 put

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 ).然而,情况并非总是如此,过去cout只是在全局命名空间中(或者,后来,在 global 和std )。 I would therefore conclude that your classes used an older C++ compiler.因此,我认为您的类使用了较旧的 C++ 编译器。

Everything in the Standard Template/Iostream Library resides in namespace std.标准模板/Iostream 库中的所有内容都位于命名空间 std 中。 You've probably used:你可能用过:

using namespace std;

In your classes, and that's why it worked.在你的课堂上,这就是它奏效的原因。

You probably had using namespace std;您可能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:: .这明确告诉预编译器在std查找符号,这意味着您不需要std:: Though it is good practice to std::cout instead of cout so you explicitly invoke std::cout every time.尽管使用std::cout而不是cout是一个好习惯,因此您每次都显式调用std::cout That way if you are using another library that redefines cout , you still have the std::cout behavior instead of some other custom behavior.这样,如果您使用另一个重新定义cout库,您仍然拥有std::cout行为而不是其他一些自定义行为。

"std" is a namespace used for STL (Standard Template Library). “std”是用于 STL(标准模板库)的命名空间。 Please refer tohttps://en.wikipedia.org/wiki/Namespace#Use_in_common_languages请参考https://en.wikipedia.org/wiki/Namespace#Use_in_common_languages

You can either write using namespace std;您可以using namespace std;编写using namespace std; before using any stl functions, variables or just insert std:: before them.在使用任何 stl 函数、变量之前,或者只是在它们之前插入std::

If you are working in ROOT, you do not even have to write #include<iostream> and using namespace std;如果您在 ROOT 下工作,您甚至不必编写#include<iostream>using namespace std; simply start from int filename() .只需从int filename()

This will solve the issue.这将解决问题。

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

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