错误C2065:'cout':未声明的标识符

Wal*_*ter 67 c++ namespaces std visual-studio-2010-beta-2

我正在编写我的编程任务的"驱动程序"部分,我不断得到这个荒谬的错误:

错误C2065:'cout':未声明的标识符

我甚至尝试过使用std :: cout,但我得到另一个错误:IntelliSense:命名空间"std"没有成员"cout"当我声明使用命名空间std,包括iostream +我甚至试图使用ostream

我知道这是一个标准的菜鸟问题,但这让我感到难过,我是一个新手(意思是:我之前已经编程了......)

#include <iostream>
using namespace std;

int main () {
    cout << "hey" << endl;
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Visual Studio 2010并运行Windows 7.所有.h文件都有"using namespace std"并包含iostream和ostream.

Geo*_*ond 63

在Visual Studio中,您必须#include "stdafx.h"成为cpp文件的第一个包含.例如:

这些将不会工作.

#include <iostream>
using namespace std;
int main () {
    cout << "hey" << endl;
    return 0;
}




#include <iostream>
#include "stdafx.h"
using namespace std;
int main () {
    cout << "hey" << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这样做.

#include "stdafx.h"
#include <iostream>
using namespace std;
int main () {
    cout << "hey" << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是stdafx.h头文件的一个很好的答案.

  • 这应该是正确的答案,因为它指定了代码的工作原理.非常感谢. (3认同)

Hri*_*ari 47

写这段代码,它完美地工作..

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
 cout<<"Hello World!";
  return 0;
}
Run Code Online (Sandbox Code Playgroud)


小智 10

我在Visual Studio C++ 2010上遇到了同样的问题.它很容易修复.在main()函数上方,只需用下面的替换标准include行,但在includes前面加上井号.

# include "stdafx.h"
# include <iostream>
using  namespace std;
Run Code Online (Sandbox Code Playgroud)


小智 8

include "stdafx.h"是确定

cout除非你包括在内,否则你不能使用using namespace std

如果你没有包含namespace std,你必须编写std::cout而不是简单cout


Jon*_*onH 2

在开始该程序之前,请删除所有代码并在 main 中执行一个简单的 hello world。仅包含 iostream 和 using namespace std;。一点一点地添加它以找到您的问题。

cout << "hi" << endl;
Run Code Online (Sandbox Code Playgroud)