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

2021-12-14 00:00:00 namespaces c++ visual-studio-2010 std

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

I am working on the 'driver' part of my programing assignment and i keep getting this absurd error:

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

error C2065: 'cout' : undeclared identifier

我什至尝试过使用 std::cout 但我收到另一个错误消息:IntelliSense: namespace "std" has no member "cout" 当我有声明 using namespace std,包括 iostream + 我什至尝试使用 ostream

I have even tried using the std::cout but i get another error that says: IntelliSense: namespace "std" has no member "cout" when i have declared using namespace std, included iostream + i even tried to use ostream

我知道这是一个标准的菜鸟问题,但这让我很难过,而且我是个新手(意思是:我以前编程过......)

I know it's a standard noob question but this has stumped me and I'm a novice (meaning: I've programed before...)

#include <iostream>
using namespace std;

int main () {
    cout << "hey" << endl;
 return 0;
}

我使用的是 Visual Studio 2010 并运行 Windows 7.所有 .h 文件都有使用命名空间 std"并包括 iostream 和 ostream.

I'm using Visual Studio 2010 and running Windows 7. All of the .h files have "using namespace std" and include iostream and ostream.

推荐答案

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

In Visual Studio you must #include "stdafx.h" and be the first include of the cpp file. For instance:

这些不起作用.

#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;
}

这就行了.

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

这里有一个关于 stdafx.h 标头功能的很好的答案.

相关文章