简体   繁体   English

C++ cout 给出未声明的标识符

[英]C++ cout gives undeclared identifier

So, I have this question.所以,我有这个问题。 Why does cout throws cout为什么会抛出

error C2065: 'cout' : undeclared identifier

I am using Visual Studio 2012 as an IDE and I am writing a school project.我正在使用 Visual Studio 2012 作为 IDE,并且正在编写一个学校项目。 I have everything done except an example file.除了示例文件之外,我已经完成了所有工作。 So I am trying to write something on the screen like this:所以我想在屏幕上写这样的东西:

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

int main()
{
    cout<<"example";

    return 0;
}

So the problem is with cout... printf works fine, but I want to use cout.所以问题出在 cout ... printf 工作正常,但我想使用 cout。

EDIT: I've changed "" to <> but it is not helping.编辑:我已将 "" 更改为 <> 但它没有帮助。 Also I am using this code only for example... This is not the whole project.此外,我仅使用此代码作为示例……这不是整个项目。

stdafx.h shall be the first include directive in your source file. stdafx.h应该是源文件中的第一个包含指令。

Switch files and convert the second include to <> , as other suggested.如其他建议的那样,切换文件并将第二个包含转换为<>

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

Seethis post for more information.有关更多信息,请参阅此帖子

First of all:首先:

#include <iostream>

instead of #include "iostream"而不是#include "iostream"

Secondly, it is generally considered bad practice to write using namespace std;其次, using namespace std; , even though most courses start with that. ,尽管大多数课程都是从这个开始的。 It is better to only use what you actually need, in your case:在您的情况下,最好只使用您实际需要的东西:

using std::cout;

 #include "iostream"

should be应该

 #include <iostream>

Quoting from this post: difference-between-iostream-and-iostream-quotes-in-include引用这篇文章: diff-between-iostream-and-iostream-quotes-in-include

By courtesy of @Jerry Coffin's answer:感谢@Jerry Coffin 的回答:

When you use < >, the compiler only looks in the system-designated directory/directories (eg, whatever you've set in the include environment variable) for the header.当您使用 < > 时,编译器仅在系统指定的目录/目录(例如,您在包含环境变量中设置的任何目录)中查找标头。

When you use " ", the compiler looks in the local directory first, and if that fails, re-searches just like you'd used < >.当你使用 " " 时,编译器首先在本地目录中查找,如果失败,就像你使用 < > 一样重新搜索。 Technically, (ie, according to the standard) that doesn't have to be the "local" directory, but that's how it works in essentially every compiler of which I'm aware).从技术上讲,(即,根据标准)不一定是“本地”目录,但这就是它在我所知道的每个编译器中的工作方式)。

EDIT:编辑:

However, the root cause is that stdafx.h is a precompiled header.但是,根本原因是stdafx.h是一个预编译的头文件。 Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); Visual C++ 不会编译源文件中#include "stdafx.h"之前的任何内容,除非未选中编译选项/Yu'stdafx.h' (默认情况下); it assumes all code in the source up to and including that line is already compiled.它假定源代码中直到并包括该行的所有代码都已编译。 However, it is still better to use <> with iostream not to confuse reader of the code.但是,最好将<>iostream一起使用,以免混淆代码的读者。

If you use #include <iostream> with the <> instead of "" then it should work.如果您将#include <iostream>与 <> 而不是 "" 一起使用,那么它应该可以工作。 Right now, the compiler doesn't know where to find the iostream library.现在,编译器不知道在哪里可以找到 iostream 库。

Also, you might want to change cout<<"example";此外,您可能想更改cout<<"example"; to cout<<"example"<<endl;cout<<"example"<<endl; for a new line so that it formats correctly.换行,使其格式正确。

Came across this issue while trying to build a Dynamic Linked Library.尝试构建动态链接库时遇到此问题。 Make sure that instead of the #include stdafx.h you specify the following include on the first line of your .cpp file:确保您在 .cpp 文件的第一行指定以下包含而不是#include stdafx.h

#include "pch.h"

This should also be the case for VS2017 or earlier. VS2017 或更早版本也应该如此。

This error also occurred in the Visual Studio 2017 IDE.此错误也发生在 Visual Studio 2017 IDE 中。 Moving stdafx.h to the top solved the error.将 stdafx.h 移到顶部解决了该错误。

For more on stdafx.h, see What's the use for "stdafx.h" in Visual Studio?有关 stdafx.h 的更多信息,请参阅Visual Studio 中“stdafx.h”的用途是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM