如何避免与 Windows 头文件中定义的宏发生名称冲突?

How do I avoid name collision with macros defined in Windows header files?(如何避免与 Windows 头文件中定义的宏发生名称冲突?)

本文介绍了如何避免与 Windows 头文件中定义的宏发生名称冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 C++ 代码包含一个名为 CreateDirectory() 的方法. 以前的代码只使用 STL 和 Boost,但我最近不得不包含 <windows.h>代码>,所以我可以查找 CSIDL_LOCAL_APPDATA.

I have some C++ code that includes a method called CreateDirectory(). Previously the code only used STL and Boost, but I recently had to include <windows.h> so I could look-up CSIDL_LOCAL_APPDATA.

现在,这段代码:

filesystem.CreateDirectory(p->Pathname()); // Actually create it...

不再编译:

error C2039: 'CreateDirectoryA' : is not a member of ...

对应winbase.h中的这个宏:

#ifdef UNICODE
#define CreateDirectory  CreateDirectoryW
#else
#define CreateDirectory  CreateDirectoryA
#endif // !UNICODE

预处理器正在重新定义我的方法调用.有没有办法避免这种命名冲突?还是我必须重命名我的 CreateDirectory() 方法?

The pre-processor is redefining my method call. Is there any possible way to avoid this naming collision? Or do I have to rename my CreateDirectory() method?

推荐答案

如果你只是重命名你的 CreateDirectory 方法会更好.如果您需要使用 Windows API,与 Windows.h 竞争是一场失败的战斗.

You will be better off if you just rename your CreateDirectory method. If you need to use windows APIs, fighting with Windows.h is a losing battle.

顺便说一句,如果您在包含 windows.h 时一致,它仍将被编译.(虽然你可能在其他地方有问题).

Incidently, if you were consistent in including windows.h, this will still be compiling. (although you might have problems in other places).

这篇关于如何避免与 Windows 头文件中定义的宏发生名称冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何避免与 Windows 头文件中定义的宏发生名称冲突?

基础教程推荐