【发布时间】:2014-02-12 02:45:15
【问题描述】:
我正在尝试编译下面的简单程序。但是,它没有编译并给出错误:
error C2065: 'cout' : undeclared identifier
我想问你为什么这个程序在我已经包含iostream 头文件的情况下不起作用?
#include <iostream>
void function(int) { cout << “function(int) called” << endl; }
void function(unsigned int) { cout << “function(unsigned int) called” << endl; }
int main()
{
function(-2);
function(4);
return 0;
}
提前致谢。
【问题讨论】:
-
仅使用
std::cout而不是cout。在namespace std使用的所有内容之前附加std::。 -
或者在这种情况下——当你编写一个非常简单的程序时,你总是可以在你的
#include <iostream>下面的某个地方写上using namespace std;。它将通知编译器在 std 命名空间中查找cout,从而允许您的cout工作。尽管这被认为是一种不好的做法。 -
避免
using namespace std;。那保证有一天会咬你一口。如果您不想输入std::cout,请使用using std::cout,但将其限制在较小的范围内,并且不要在标题中使用它。 -
这里有更多关于
using namespace std问题的信息:stackoverflow.com/questions/1452721/…
标签: c++ namespaces iostream cout