簡體   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