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 头文件中定义的宏发生名称冲突?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
