如何避免在Windows头文件中定义的宏与名称冲突? [英] How do I avoid name collision with macros defined in Windows header files?

查看:764
本文介绍了如何避免在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.

偶尔,如果你一致包括窗口。 h,这仍然是编译。 (虽然你可能在其他地方有问题)。

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆