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

发布于 2024-08-14 04:17:40 字数 583 浏览 4 评论 0原文

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

错误 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 并包含 iostreamostream

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(26

守望孤独 2024-08-21 04:17:40

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

这样就可以了。

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

这是关于 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.

#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.

捂风挽笑 2024-08-21 04:17:40

编写这段代码,它完美运行..

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

using namespace std;

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

write this code, it works perfectly..

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

using namespace std;

int main()
{
 cout<<"Hello World!";
  return 0;
}
大姐,你呐 2024-08-21 04:17:40

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

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

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"
# include <iostream>
using  namespace std;
毁梦 2024-08-21 04:17:40

include "stdafx.h" 没问题,

但是你不能使用 cout 除非你已经包含 using namespace std

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

The include "stdafx.h" is ok

But you can't use cout unless you have included using namespace std

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

小镇女孩 2024-08-21 04:17:40

我发现如果你使用的

#include <iostream.h>

话就会遇到问题。

如果您使用

#include <iostream>  

(注意 - 没有 .h),

那么您将不会遇到您提到的问题。

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)

then you will not get the problem you mentioned.

你げ笑在眉眼 2024-08-21 04:17:40

如果您包含的唯一文件是 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?

焚却相思 2024-08-21 04:17:40

如果您启动的项目需要 #include "stdafx.h" 行,请将其放在第一位。

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

醉酒的小男人 2024-08-21 04:17:40

当我在 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.

微暖i 2024-08-21 04:17:40

在我的例子中,这是一个愚蠢的解决方案:

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

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

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

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

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...

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

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

梦在夏天 2024-08-21 04:17:40

下面的代码使用 gcc 可以正确编译并运行。尝试复制/粘贴此内容并查看是否有效。

#include <iostream>
using namespace std;

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

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

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

#include <iostream>
using namespace std;

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

int main () {
    int a = 1;
    bob(a);
    return 0;
}
心是晴朗的。 2024-08-21 04:17:40

我有 VS2010、Beta 1 和 Beta 2(一个在我的工作机器上,一个在家里),并且我已经使用过很多 std 没有出现任何问题。尝试输入:

std::

并查看 Intellisense 是否为您提供任何信息。如果它给你通常的东西(abortabsacos等),除了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:

std::

And see if Intellisense gives you anything. If it gives you the usual stuff (abort, abs, acos, etc.), except for cout, 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?)

怀里藏娇 2024-08-21 04:17:40

代码

#include <iostream>
using namespace std;

从 .cpp 文件中取出 ,创建一个头文件并将其放入 .h 文件中。然后添加到

#include "whatever your header file is named.h"

.cpp 代码的顶部。然后再次运行它。

Take the code

#include <iostream>
using namespace std;

out of your .cpp file, create a header file and put this in the .h file. Then add

#include "whatever your header file is named.h"

at the top of your .cpp code. Then run it again.

情绪操控生活 2024-08-21 04:17:40

从头开始启动 ms c++ 2010 项目时,我遇到了同样的问题 - 我删除了 ms 生成的所有头文件,但使用了:

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

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

我必须包含 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:

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

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

I had to include stdafx.h as it caused an error not having it in.

心头的小情儿 2024-08-21 04:17:40

尝试一下,它会起作用的。我在 Windows XP、Visual Studio 2010 Express 中检查过它。

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

void main( ) 
{
   int i = 0;
   cout << "Enter a number: ";
   cin >> i;
}

Try it, it will work. I checked it in Windows XP, Visual Studio 2010 Express.

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

void main( ) 
{
   int i = 0;
   cout << "Enter a number: ";
   cin >> i;
}
杀手六號 2024-08-21 04:17:40

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

cout << "hi" << endl;

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.

cout << "hi" << endl;
谎言月老 2024-08-21 04:17:40

你确定它编译为 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 in namespace 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 a main() function.

I've got a really, really bad feeling about VS 2010.

怀中猫帐中妖 2024-08-21 04:17:40

当您创建项目时,您没有正确设置“使用预编译头”。在属性->C/C++->预编译头中更改它。

When you created your project, you did not set 'use precompiled headers' correctly. Change it in properties->C/C++->precompiled headers.

清风不识月 2024-08-21 04:17:40

在 Visual Studio 中,使用“stdafx.h”下面的所有头文件管理器。

In Visual studio use all your header filer below "stdafx.h".

请别遗忘我 2024-08-21 04:17:40

只需使用printf

stdio.h 包含在 printfstdafx.h 头文件中。

Just use printf!

Include stdio.h in your stdafx.h header file for printf.

葬花如无物 2024-08-21 04:17:40

通过在代码顶部插入以下行来包含 std 库:

using namespace std;

Include the std library by inserting the following line at the top of your code:

using namespace std;
寒尘 2024-08-21 04:17:40

通常存储在 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.

我ぃ本無心為│何有愛 2024-08-21 04:17:40

我在安装 vs 2010 并试图让一个几乎相同的程序运行后遇到了这个错误。

我之前在 UNIX 风格的机器上做过普通 C 编码,决定自己尝试一下。

我尝试的第一个程序是:

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Hello World!";
    return 0;
}

这里需要注意的一件大事...如果您曾经做过任何 C 编码,

int _tmain(int argc, _TCHAR* argv[])

看起来很奇怪。它应该是:

int main( int argc, char ** argv )

就我而言,我只是将程序更改为:

#include <iostream>
using namespace std;

int main()
{
     cout << "Hello world from  VS 2010!\n";
     return 0;
}

它工作得很好。

注意:使用 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:

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Hello World!";
    return 0;
}

The big thing to notice here... if you've EVER done any C coding,

int _tmain(int argc, _TCHAR* argv[])

Looks weird. it should be:

int main( int argc, char ** argv )

In my case I just changed the program to:

#include <iostream>
using namespace std;

int main()
{
     cout << "Hello world from  VS 2010!\n";
     return 0;
}

And it worked fine.

Note: Use CTRL + F5 so that the console window sticks around so you can see the results.

燕归巢 2024-08-21 04:17:40

我来到这里是因为我遇到了同样的问题,但是当我执行 #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

时光暖心i 2024-08-21 04:17:40

遇到这个问题,当头文件声明“使用命名空间 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.

遮云壑 2024-08-21 04:17:40

在 VS2017 中,stdafx.h 似乎被 pch.h 取代,请参阅这篇文章

所以使用:

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

using namespace std;

int main() {
    cout << "Enter 2 numbers:" << endl;

In VS2017, stdafx.h seems to be replaced by pch.h see this article,

so use:

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

using namespace std;

int main() {
    cout << "Enter 2 numbers:" << endl;
茶花眉 2024-08-21 04:17:40

这是编译器 - 我现在使用 Eclipse Galileo,该程序运行得非常好



It was the compiler - I'm now using Eclipse Galileo and the program works like a wonder


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文