错误 C2065:“cout” : 未声明的标识符
我正在处理我的编程作业的“驱动程序”部分,但我不断收到这个荒谬的错误:
错误 C2065:“cout”:未声明的标识符
我什至尝试使用 std::cout
但我收到另一个错误:
IntelliSense:命名空间“std”没有成员“cout”
当我声明使用命名空间std,包括iostream
并且我什至尝试使用ostream
时,
#include <iostream>
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
我正在使用Visual Studio 2010并运行 Windows 7。所有 .h
文件都具有 using namespace std
并包含 iostream
和 ostream
。
I am working on the 'driver' part of my programing assignment and i keep getting this absurd error:
error C2065: 'cout' : undeclared identifier
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
and I even tried to use ostream
#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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(26)
在 Visual Studio 中,您必须
#include "stdafx.h"
并且第一个包含 cpp 文件。例如:这些将不起作用 。
这样就可以了。
这是关于 stdafx.h 标头的作用的一个很好的答案。
In Visual Studio you must
#include "stdafx.h"
and be the first include of the cpp file. For instance:These will not work.
This will do.
Here is a great answer on what the stdafx.h header does.
编写这段代码,它完美运行..
write this code, it works perfectly..
我在 Visual Studio C++ 2010 上遇到了同样的问题。很容易修复。在 main() 函数上方,只需用下面的代码替换标准包含行,但在包含前面添加井号。
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.
include "stdafx.h"
没问题,但是你不能使用
cout
除非你已经包含using namespace std
如果你还没有包含命名空间std 你必须写
std::cout
而不是简单的cout
The
include "stdafx.h"
is okBut you can't use
cout
unless you have includedusing namespace std
If you have not included namespace std you have to write
std::cout
instead of simplecout
我发现如果你使用的
话就会遇到问题。
如果您使用
(注意 - 没有 .h),
那么您将不会遇到您提到的问题。
I have seen that if you use
then you will get the problem.
If you use
(notice - without the .h)
then you will not get the problem you mentioned.
如果您包含的唯一文件是 iostream 并且它仍然显示未定义,那么 iostream 可能不包含它应该包含的内容。您的项目中是否有可能有一个名为“iostream”的空文件?
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?
如果您启动的项目需要
#include "stdafx.h"
行,请将其放在第一位。If you started a project requiring the
#include "stdafx.h"
line, put it first.当我在 C++ 代码中使用 .c 文件扩展名时,我看到过类似的情况发生。除此之外,我必须同意每个人关于错误安装的看法。如果您尝试使用早期版本的 VS 编译该项目是否有效?尝试 VC++ Express 2008。它在 msdn 上是免费的。
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.
在我的例子中,这是一个愚蠢的解决方案:
上面的代码是按照示例a进行排序的,当我将其更改为类似于下面的示例b时......
我的代码编译得像一个魅力。尝试一下,保证有效。
Such a silly solution in my case:
The above was odered as per example a, when I changed it to resemble example b below...
My code compiled like a charm. Try it, guaranteed to work.
下面的代码使用 gcc 可以正确编译并运行。尝试复制/粘贴此内容并查看是否有效。
The code below compiles and runs properly for me using gcc. Try copy/pasting this and see if it works.
我有 VS2010、Beta 1 和 Beta 2(一个在我的工作机器上,一个在家里),并且我已经使用过很多
std
没有出现任何问题。尝试输入:并查看 Intellisense 是否为您提供任何信息。如果它给你通常的东西(
abort
、abs
、acos
等),除了cout
,那么,那真是一个谜题。在这种情况下,一定要检查你的 C++ 头文件。
除此之外,我想补充一点,确保您正在运行一个常规的空项目(不是 CLR,其中 Intellisense 被削弱),并且您实际上已经尝试构建该项目至少一次。正如我在评论中提到的,一旦您添加了
include
;,VS2010 就会解析文件。可能是某些东西卡住了解析器并且它没有立即“找到”cout
。 (这种情况可以尝试重启VS吗?)I have VS2010, Beta 1 and Beta 2 (one on my work machine and one at home), and I've used
std
plenty without issues. Try typing:And see if Intellisense gives you anything. If it gives you the usual stuff (
abort
,abs
,acos
, etc.), except forcout
, well then, that is quite a puzzler. Definitely look into your C++ headers in that case.Beyond that, I would just add to make sure you're running a regular, empty project (not CLR, where Intellisense is crippled), and that you've actually attempted to build the project at least once. As I mentioned in a comment, VS2010 parses files once you've added an
include
; it could be that something stuck the parser and it didn't "find"cout
right away. (In which case, try restarting VS maybe?)代码
从 .cpp 文件中取出 ,创建一个头文件并将其放入 .h 文件中。然后添加到
.cpp 代码的顶部。然后再次运行它。
Take the code
out of your .cpp file, create a header file and put this in the .h file. Then add
at the top of your .cpp code. Then run it again.
从头开始启动 ms c++ 2010 项目时,我遇到了同样的问题 - 我删除了 ms 生成的所有头文件,但使用了:
我必须包含
stdafx.h
因为它导致错误没有它在。I had the same issue when starting a ms c++ 2010 project from scratch - I removed all of the header files generated by ms and but used:
I had to include
stdafx.h
as it caused an error not having it in.尝试一下,它会起作用的。我在 Windows XP、Visual Studio 2010 Express 中检查过它。
Try it, it will work. I checked it in Windows XP, Visual Studio 2010 Express.
在开始该程序之前,请删除所有代码并在 main 中执行一个简单的 hello world。仅包含 iostream 和 using namespace std;。
一点一点地添加它以找到您的问题。
before you begin this program get rid of all the code and do a simple hello world inside of main. Only include iostream and using namespace std;.
Little by little add to it to find your issue.
你确定它编译为 C++ 吗?检查您的文件名(应以
.cpp
结尾)。检查您的项目设置。您的程序没有任何问题,并且
cout
位于namespace std
中。您安装的 VS 2010 Beta 2 有缺陷,我认为这不仅仅是您的安装。我认为 VS 2010 还没有为 C++ 做好准备。标准的“Hello, World”程序在 Beta 1 上不起作用。我只是尝试创建一个测试 Win32 控制台应用程序,生成的
test.cpp
文件没有main( )
函数。我对 VS 2010 有一种非常非常不好的感觉。
Are you sure it's compiling as C++? Check your file name (it should end in
.cpp
). Check your project settings.There's simply nothing wrong with your program, and
cout
is innamespace std
. Your installation of VS 2010 Beta 2 is defective, and I don't think it's just your installation.I don't think VS 2010 is ready for C++ yet. The standard "Hello, World" program didn't work on Beta 1. I just tried creating a test Win32 console application, and the generated
test.cpp
file didn't have amain()
function.I've got a really, really bad feeling about VS 2010.
当您创建项目时,您没有正确设置“使用预编译头”。在属性->C/C++->预编译头中更改它。
When you created your project, you did not set 'use precompiled headers' correctly. Change it in properties->C/C++->precompiled headers.
在 Visual Studio 中,使用“stdafx.h”下面的所有头文件管理器。
In Visual studio use all your header filer below "stdafx.h".
只需使用
printf
!将
stdio.h
包含在printf
的stdafx.h
头文件中。Just use
printf
!Include
stdio.h
in yourstdafx.h
header file forprintf
.通过在代码顶部插入以下行来包含 std 库:
Include the std library by inserting the following line at the top of your code:
通常存储在 C:\Program Files\Microsoft Visual Studio 8\VC\include 文件夹中。首先检查它是否还在。然后选择“工具+选项”、“项目和解决方案”、“VC++ 目录”,在“显示目录”组合框中选择“包含文件”,并仔细检查 $(VCInstallDir)include 是否位于列表顶部。
is normally stored in the C:\Program Files\Microsoft Visual Studio 8\VC\include folder. First check if it is still there. Then choose Tools + Options, Projects and Solutions, VC++ Directories, choose "Include files" in the "Show Directories for" combobox and double-check that $(VCInstallDir)include is on top of the list.
我在安装 vs 2010 并试图让一个几乎相同的程序运行后遇到了这个错误。
我之前在 UNIX 风格的机器上做过普通 C 编码,决定自己尝试一下。
我尝试的第一个程序是:
这里需要注意的一件大事...如果您曾经做过任何 C 编码,
看起来很奇怪。它应该是:
就我而言,我只是将程序更改为:
它工作得很好。
注意:使用 CTRL + F5 使控制台窗口保持不变,以便您可以看到结果。
I ran across this error after just having installed vs 2010 and just trying to get a nearly identical program to work.
I've done vanilla C coding on unix-style boxes before, decided I'd play with this a bit myself.
The first program I tried was:
The big thing to notice here... if you've EVER done any C coding,
Looks weird. it should be:
In my case I just changed the program to:
And it worked fine.
Note: Use CTRL + F5 so that the console window sticks around so you can see the results.
我来到这里是因为我遇到了同样的问题,但是当我执行
#include "stdafx.h"
时,它说找不到该文件。对我来说,诀窍是:
#include
。我使用 Microsoft Visual Studio 2008。
这些是您可以使用的东西,包括。 'count': 链接
I came here because I had the same problem, but when I did
#include "stdafx.h"
it said it did not find that file.What did the trick for me was:
#include <algorithm>
.I use Microsoft Visual Studio 2008.
These are the things that you can use then, incl. 'count': Link
遇到这个问题,当头文件声明“使用命名空间 std;”时,GNU 编译器似乎感到困惑;
反正就是风格不好!
解决方案是在标头中提供 std::cout ... 并将“使用命名空间 std”移至实现文件。
Had this problem, when header files declared "using namespace std;", seems to be confusing for GNU compiler;
anyway is bad style!
Solution was providing std::cout ... in headers and moving "using namespace std" to the implementation file.
在 VS2017 中,
stdafx.h
似乎被pch.h
取代,请参阅这篇文章,所以使用:
In VS2017,
stdafx.h
seems to be replaced bypch.h
see this article,so use:
这是编译器 - 我现在使用 Eclipse Galileo,该程序运行得非常好
It was the compiler - I'm now using Eclipse Galileo and the program works like a wonder