简体   繁体   English

#include iostream 在 c++ 中的 stdafx.h 之前

[英]#include iostream before stdafx.h in c++

I created a C++ Console Application in Visual Studio Community 2017. There is only a main.cpp file in the project.我在Visual Studio Community 2017中创建了一个C++ Console Application,项目中只有一个main.cpp文件。 Here is my main.cpp file:这是我的 main.cpp 文件:

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

int main()
{
    std::cout << "hello world!";
    return 0;
}

I get a compilation error that 'cout' is not a member of std.我收到一个编译错误,指出 'cout' 不是 std 的成员。 But if I include iostream after stdafx.h, that is,但是如果我在 stdafx.h 之后包含 iostream,那就是

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

int main()
{
    std::cout << "hello world!";
    return 0;
}

then it compiles just fine.然后它编译就好了。 So why does it not work when I include iostream before stdafx.h?那么为什么当我在 stdafx.h 之前包含 iostream 时它不起作用?

The answer to your question can be found, with a little puzzling, here .在回答你的问题可以发现,有一点令人困惑, 在这里

stdafx.h enables precompiled headers. stdafx.h 启用预编译头文件。 Based on the error given, and the discussion of how Microsoft implements precompiled headers, it seems that the compiler simply starts compiling from the include of stdafx.h forward.根据给出的错误,以及微软如何实现预编译头文件的讨论,编译器似乎只是从 stdafx.h 的包含开始编译。 So when stdafx.h is placed after iostream, iostream is not included, producing the mysterious error.因此,当 stdafx.h 放在 iostream 之后时,iostream 不包括在内,从而产生了神秘的错误。

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

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