#define 宏和枚举使用相同名称引起的冲突

Ric*_*ams 5 c++ x11 enums physx

我正在尝试将 NVIDIA 的 PhysX 集成到我的 Linux 代码库中。在它的一些头文件中,它定义了以下枚举:

\n\n

physxvisualdebuggersdk/PvdErrorCodes.h

\n\n
struct PvdErrorType\n{\n  enum Enum\n  {\n    Success = 0,\n    NetworkError,\n    ArgumentError,\n    InternalProblem\n  };\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n

physxprofilesdk/PxProfileCompileTimeEventFilter.h:

\n\n
struct EventPriorities\n{\n  enum Enum\n  {\n    None,       // the filter setting to kill all events\n    Coarse,\n    Medium,\n    Detail,\n    Never       // the priority to set for an event if it should never fire.\n  };\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n

这会导致以下编译错误:

\n\n
/usr/local/include/PhysX3/physxvisualdebuggersdk/PvdErrorCodes.h:36:4: error: expected identifier before numeric constant\n    Success = 0,\n    ^\n/usr/local/include/PhysX3/physxvisualdebuggersdk/PvdErrorCodes.h:36:4: error: expected \xe2\x80\x98}\xe2\x80\x99 before numeric constant\n/usr/local/include/PhysX3/physxvisualdebuggersdk/PvdErrorCodes.h:36:4: error: expected unqualified-id before numeric constant\n
Run Code Online (Sandbox Code Playgroud)\n\n

\n\n
/usr/local/include/PhysX3/physxprofilesdk/PxProfileCompileTimeEventFilter.h:46:4: error: expected identifier before numeric constant\n    None,  // the filter setting to kill all events\n    ^\n/usr/local/include/PhysX3/physxprofilesdk/PxProfileCompileTimeEventFilter.h:46:4: error: expected \xe2\x80\x98}\xe2\x80\x99 before numeric constant\n/usr/local/include/PhysX3/physxprofilesdk/PxProfileCompileTimeEventFilter.h:46:4: error: expected unqualified-id before numeric constant\n
Run Code Online (Sandbox Code Playgroud)\n\n

我确定这是因为X11/Xh #定义了“无”和“成功”。我已经确认这是问题所在,因为如果我 #undef 'None' 和 'Success',我就不再有错误。然而,这显然不是一件可取的事情。

\n\n

我的问题是:作为必须使用这两个标头的开发人员,我应该采取的正确做法是什么?我应该将其作为错误报告给 NVIDIA 并等待修复,还是我可以自己做一些事情来解决该问题(除了#undef)?

\n\n

谢谢你的时间!

\n

Mar*_*k B 1

最明智的做法是以这样的方式分离实现,即在项目中不要在同一源文件中包含两个冲突的标头。一个文件处理 X,一个文件处理 PhysX,然后您的应用程序将这两个实现联系在一起。