C++ 错误 C2065:“cout”:未声明的标识符

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

error C2065: 'cout' : undeclared identifier

c++namespacesvisual-studio-2010std

提问by Wallter

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

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

error C2065: 'cout' : undeclared identifier

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

I have even tried using the std::coutbut 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

我什至尝试过使用std::cout,但我收到另一个错误:IntelliSense:命名空间“std”没有成员“cout”,当我声明使用命名空间 std,包括 iostream+ 我什至尝试使用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;
}

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 2010 并运行 Windows 7。所有 .h 文件都具有“使用命名空间 std”并包括 iostream 和 ostream。

回答by George Chondrompilas

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

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

These will notwork.

这些将不会工作。

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

This will do.

这会做。

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

Here is a great answer on what the stdafx.h header does.

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

回答by Hrishikesh Choudhari

write this code, it works perfectly..

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

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

using namespace std;

int main()
{
 cout<<"Hello World!";
  return 0;
}

回答by mee

I had same problem on Visual Studio C++ 2010. It's easy to fix. Above the main() function just replace the standard include lines with this below but with the pound symbol in front of the includes.

我在 Visual Studio C++ 2010 上遇到了同样的问题。很容易修复。在 main() 函数上方,只需将标准包含行替换为下面的行,但在包含之前使用磅符号。

# include "stdafx.h"
# include <iostream>
using  namespace std;

回答by mee

The include "stdafx.h"is ok

include "stdafx.h"是确定

But you can't use coutunless you have included using namespace std

但是cout除非您已包含,否则您无法使用using namespace std

If you have not included namespace std you have to write std::coutinstead of simple cout

如果你还没有包含命名空间 std 你必须写std::cout而不是简单cout

回答by G B

I have seen that if you use

我已经看到如果你使用

#include <iostream.h>

then you will get the problem.

那么你就会遇到问题。

If you use

如果你使用

#include <iostream>  

(notice - without the .h)

(注意 - 没有 .h)

then you will not get the problem you mentioned.

那么你就不会遇到你提到的问题。

回答by Jerrey

If you started a project requiring the #include "stdafx.h"line, put it first.

如果您开始了一个需要该#include "stdafx.h"线路的项目,请将其放在首位。

回答by Glen

The code below compiles and runs properly for me using gcc. Try copy/pasting this and see if it works.

下面的代码使用 gcc 为我编译并正确运行。尝试复制/粘贴它,看看它是否有效。

#include <iostream>
using namespace std;

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

int main () {
    int a = 1;
    bob(a);
    return 0;
}

回答by Bob Lied

If the only file you include is iostream and it still says undefined, then maybe iostream doesn't contain what it's supposed to. Is it possible that you have an empty file coincidentally named "iostream" in your project?

如果您包含的唯一文件是 iostream 并且它仍然显示未定义,那么可能 iostream 不包含它应该包含的内容。您的项目中是否有一个巧合名为“iostream”的空文件?

回答by goatlinks

I've seen similar things happen when I was using the .c file extension with C++ code. Other than that, I'd have to agree with everyone about a buggy installation. Does it work if you try to compile the project with an earlier release of VS? Try VC++ Express 2008. Its free on msdn.

当我将 .c 文件扩展名与 C++ 代码一起使用时,我看到过类似的事情发生。除此之外,我必须同意每个人关于错误安装的看法。如果您尝试使用较早版本的 VS 编译项目,它是否有效?试试 VC++ Express 2008。它在 msdn 上免费。

回答by Sizons

Such a silly solution in my case:

在我的情况下,这样一个愚蠢的解决方案:

// Example a
#include <iostream>    
#include "stdafx.h"

The above was odered as per example a, when I changed it to resemble example b below...

上面是按照示例 a 进行的,当我将其更改为类似于下面的示例 b 时...

// Example b
#include "stdafx.h"
#include <iostream>  

My code compiled like a charm. Try it, guaranteed to work.

我的代码编译得像个魅力。试试看,保证有效。