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 头文件中定义的宏发生名称冲突?
基础教程推荐
- 提升 ASIO 流缓冲 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- c++ STL设置差异 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
