繁体   English   中英

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

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

我在Visual Studio Community 2017中创建了一个C++ Console Application,项目中只有一个main.cpp文件。 这是我的 main.cpp 文件:

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

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

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

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

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

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

在回答你的问题可以发现,有一点令人困惑, 在这里

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

暂无
暂无

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

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