【问题标题】:#include iostream before stdafx.h in c++#include iostream 在 C++ 中的 stdafx.h 之前
【发布时间】:2018-10-12 11:10:21
【问题描述】:

我在 Visual Studio Community 2017 中创建了一个 C++ 控制台应用程序。项目中只有一个 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 时,为什么它不起作用?

【问题讨论】:

  • 关于 SO 上的预编译头文件有很多问题,我不确定将其中哪一个标记为重复。
  • 在我的项目中,其他开发人员之一关闭了预编译头文件,并将我们的构建时间缩短了一半。
  • @Eljay 这取决于项目。在我正在处理的项目上 - 完全重建需要 2 个多小时。如果不花费大量时间,甚至无法测试预编译的标头如何影响构建性能。
  • @Eljay 在这种情况下,您很可能在预编译中包含错误的标头。只有多次包含的标头才能真正受益。

标签: c++ visual-studio include console-application


【解决方案1】:

可以找到你的问题的答案,有点不解,here

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

【讨论】:

    猜你喜欢
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多