我尝试在 Mac 上的 Sublime Text 2 上用 C++ 运行 hello world。
I typed
#include "iostream"
int main()
{
cout<<"Hello WOrld";
return (0);
}
但这给了我一个错误
/Users/chiragchaplot/q2.cpp:5:2: error: use of undeclared identifier 'cout'; did you mean 'std::cout'?
cout<<"Hello World";
^~~~
std::cout
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:50:33: note: 'std::cout' declared here
extern _LIBCPP_FUNC_VIS ostream cout;
^
1 error generated.
[Finished in 0.4s with exit code 1]
以下方法将解决您的问题:
Method 1:(不好的做法)
在 main 函数之前添加以下行。
using namespace std;
所以你的代码现在将变成:
#include "iostream"
using namespace std;
int main(){
cout << "Hello WOrld";
return (0);
}
Method 2:(良好实践)
你可以简单地写std::cout
代替cout
.
完整代码与std::cout
#include "iostream"
int main(){
std :: cout << "Hello WOrld";
return (0);
}
这告诉编译器cout
标识符来自std
命名空间。
方法2比方法1更好。进一步阅读:为什么“使用命名空间 std”被认为是不好的做法? https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice#
有关命名空间的更多信息,请查看以下链接:
- 命名空间维基百科 http://en.wikipedia.org/wiki/Namespace
- 有趣的问答@cplusplus.com http://www.cplusplus.com/forum/beginner/49748/
- 有趣的问答@devshed.com http://forums.devshed.com/programming-42/using-std-namespace-mean-45679.html
- 教程点 http://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)