问题描述
我有 MyClass.cpp
和 MyClass.h
文件.由于 MyClass.cpp
有 #include "MyClass.h"
我把 stdafx.h
放到 MyClass.h
.在编译过程中出现错误:
I have MyClass.cpp
and MyClass.h
files. Since MyClass.cpp
has #include "MyClass.h"
I put stdafx.h
to MyClass.h
. During compile I got error:
在查找预编译头时出现意外的文件结尾.您是否忘记将#includestdafx.h""添加到您的源代码中?
为什么?如果 MyClass.cpp
包含 MyClass.h
它应该包含 MyClass.h
的所有行并且这些行包含 stdafx.h.
Why? If
MyClass.cpp
includes MyClass.h
it should include all lines of MyClass.h
and these lines contain stdafx.h
.
推荐答案
是否将
#include "stdafx.h"
放在 .h 文件中并不重要.编译器不在乎.它希望将 #include "stdafx.h"
视为 .cpp 文件的第一行,除非您将不使用预编译头"作为源文件的编译器选项.推理有与预编译头有关.我无法真正提供技术性解释,但简单地这样做会让生活更轻松.
It doesn't matter if you put
#include "stdafx.h"
inside your .h file. The compiler doesn't care. It wants to see #include "stdafx.h"
as the first line of your .cpp file, unless you have "Not using precompiled headers" as a compiler option for your source file.. The reasoning has to do with precompiled headers. I cannot really offer a technical explanation, but simply doing it that way will make life easier.
这篇关于stdafx.h 到头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!