在C ++中的stdafx.h之前#include iostream [英] #include iostream before stdafx.h in c++

查看:101
本文介绍了在C ++中的stdafx.h之前#include iostream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio Community 2017中创建了一个C ++控制台应用程序.项目中只有一个main.cpp文件.这是我的main.cpp文件:

I created a C++ Console Application in Visual Studio Community 2017. There is only a main.cpp file in the project. Here is my main.cpp file:

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

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

我收到一个编译错误,提示'cout'不是std的成员.但是,如果我在stdafx.h之后包含iostream,那就是

I get a compilation error that 'cout' is not a member of std. But if I include iostream after stdafx.h, that is,

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

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

然后编译就可以了.那么,当我在stdafx.h之前包含iostream时,为什么它不起作用?

then it compiles just fine. So why does it not work when I include iostream before stdafx.h?

推荐答案

对于您问题的答案,有些令人困惑,可以找到

The answer to your question can be found, with a little puzzling, here.

stdafx.h启用预编译的头文件.根据给出的错误以及对Microsoft如何实现预编译头的讨论,似乎编译器只是从stdafx.h的包含开始进行编译.因此,将stdafx.h放在iostream之后时,将不包含iostream,从而产生神秘的错误.

stdafx.h enables precompiled headers. 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. So when stdafx.h is placed after iostream, iostream is not included, producing the mysterious error.

这篇关于在C ++中的stdafx.h之前#include iostream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆