简体   繁体   中英

stdafx.h to header files

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:

unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?

Why? If MyClass.cpp includes MyClass.h it should include all lines of MyClass.h and these lines contain stdafx.h .

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.

It is the other way around if you want MyClass.h to be precompiled.

  • MyClass.h doesn't include stdafx.h
  • stdafx.h include MyClass.h
  • MyClass.cpp include stdafx.h as the first directive. For code clarity, it may also include MyClass.h .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM