当我为 Visual C++ 项目的头文件添加包含保护时,它会给我以下警告和错误:
warning C4603: '_MAPTEST_H' : macro is not defined or definition is different after precompiled header use
Add macro to precompiled header instead of defining here
.\MapTest.cpp(6) : use of precompiled header** // the precompiled header stdafx.h is included in this line
.\MapTest.cpp(186) : fatal error C1020: unexpected #endif
但是当我在 include guard 之前添加预编译 header 时,没有发出警告或错误。这是什么原因?
最佳答案
我能想到的两个问题:
根据 this , Visual C++ 不会在您包含
stdafx.h
的行之前编译任何内容 - 因此该行必须是文件中的第一行。如果您将它放在宏定义之后,它会被跳过,因此您会看到错误。以前导下划线和大写字母(或双前导下划线)开头的标识符是保留的,这可能会导致名称冲突。见this answer更多细节。
关于c++ - 包含守卫的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2941736/