在下面的简单程序中,
#include<conio.h>
#include "stdafx.h"
#include<stdio.h>
int main(int argc, _TCHAR* argv[])
{
printf("print");
getch();
return 0;
}
给出错误提示 1>e:\cust\a\a\a.cpp(14) : error C3861: 'getch': identifier not found
我使用的是 VS 2005,我知道“stdafx.h”是预编译头文件,但为什么我们不应该在它之前添加任何头文件呢?
最佳答案
VS 中预编译 header 的工作方式是预编译 header (通常命名为 stdafx.h
)必须是包含的第一个 header 。更重要的是,行
#include "stdafx.h"
应该是源代码中的第一件事,因为 MSDN 说“编译器将 .h 文件之前出现的所有代码视为预编译。它跳到与 .h 文件关联的 #include 指令之外,使用包含的代码在.pch文件中,然后编译文件名后的所有代码。”。
关于c++ - 预编译头文件的行为导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25016806/