首页 文章

#include在cd中stdafx.h之前的iostream

提问于
浏览
-1

我在Visual Studio社区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时它不起作用?

1 回答

  • 3

    你的问题的答案可以找到,有点令人费解,here .

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

相关问题