Xcode 新手,不能使用 cout,只有 std::cout 有效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19202531/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:01:13  来源:igfitidea点击:

New to Xcode, can't use cout, only std::cout works

c++xcodemacosstdcout

提问by FoxMcWeezer

I'm not new to programming but I am new to C++ using Xcode as the IDE. I am not allowed to use cout << "hello"; to print, the compiler complains and only works when I use std:cout <<. What setting in Xcode should be fixed in order to get this working the way I'm supposed to write it for class? Thanks!

我对编程并不陌生,但我是使用 Xcode 作为 IDE 的 C++ 新手。我不允许使用 cout << "hello"; 要打印,编译器会抱怨并且仅在我使用 std:cout << 时才起作用。应该修复 Xcode 中的哪些设置才能使其按照我应该为课堂编写的方式工作?谢谢!

回答by rici

Add using std::cout;somewhere close to the start of your program, after the includes.

添加using std::cout;靠近程序开头的某个地方,在includes.

Please avoid the temptation to use using namespace std;. It's really a bad habit. Just because you see other people doing it, doesn't make it right. See the FAQ: Why is "using namespace std" considered bad practice?

请避免使用的诱惑using namespace std;。这真的是一个坏习惯。仅仅因为你看到其他人在做,并不意味着它是对的。请参阅常见问题解答:为什么“使用命名空间 std”被认为是不好的做法?

(Or, of course, you could just get use to typing std::cout.)

(或者,当然,您可以习惯键入std::cout。)

回答by saif almazrouei

I had the same problem but I fixed it. You can simply write the following after the #include:

我有同样的问题,但我修复了它。您可以在 之后简单地写下以下内容#include

using namespace std;