How do I avoid name collision with macros defined in Windows header files?(如何避免与 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 头文件中定义的宏发生名称冲突?


基础教程推荐
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01