我如何在我的命名空间中使用LoadString宏
How can I use the LoadString macros in my namespace?
当我在命名空间中使用LoadString
WinAPI宏时,我有一个问题。我的功能:
namespace Bushman {
// Get the string from the resource's string table of the module.
// I use the same name like WinAPI macros but with own signature.
PTSTR LoadString(HMODULE h, DWORD id) {
h = NULL == h ? ::GetModuleHandle(NULL) : h;
PTSTR ptr = NULL;
// it returns really length instead of concatenated
// string length, therefore I can use it for malloc.
int i = ::LoadString(h, id, (PTSTR)&ptr, 0);
if (0 == i) {
return NULL;
}
PTSTR string = (PTSTR)malloc(i * (sizeof(TCHAR) + 1));
::LoadString(h, id, string, i + 1);
return string; // NOTE: don't forget free resource in the outer code.
}
}
我得到编译错误:
'LoadStringW':不是'Bushman'的成员
'LoadStringW':不是'Bushman'的成员
'LoadStringW':不属于"布什曼"
我该如何修复它?
乌利希期刊指南
我认为问题是宏有这样的定义
#ifdef UNICODE
#define LoadString LoadStringW
#else
#define LoadString LoadStringA
#endif // !UNICODE
而不是像这样:
#ifdef UNICODE
#define LoadString ::LoadStringW
#else
#define LoadString ::LoadStringA
#endif // !UNICODE
乌利希期刊指南2
我找到了问题的原因。问题在我代码的其他地方。我在我的代码中使用了这样的声明:namespace Bushman {} // namespace declaration
PTSTR Bushman::LoadString(HMODULE h, DWORD id); // function declaration
,但它是错误的。如果我把它重写成这样,一切都可以正常工作:
namespace Bushman {
PTSTR LoadString(HMODULE h, DWORD id);
}
您有几个选择:
- 不包含定义
LoadString
宏的Windows头文件,或者 - 包含头文件,但不定义
#undef
宏。
这个问题真的没有好的解决办法。一旦你开始使用宏,你就失去了隔离和控制它们影响的能力。预处理器不关心您的名称空间。
关于你对问题的更新,你并没有真正解决问题。正如UPD2中的代码一样,预处理器将LoadString
转换为LoadStringW
。你只是没有意识到这一点,因为它在编译过程中是透明的。但是,如果您尝试从另一个没有定义LoadString
宏的翻译单元使用您的类,您将发现该函数被命名为LoadStringW
。
相关文章:
- 命名空间中具有.h和.cpp文件的类
- 从父命名空间重载类型
- 在命名空间中定义函数还是限定函数
- C++:对不存在的命名空间使用命名空间指令
- 通过继承类使用来自不同命名空间的运算符
- 使用命名空间时出现多个定义错误
- CUDA内核和数学函数的显式命名空间
- 嵌套的匿名命名空间
- CMakeLists.txt中的命名空间表示法
- 类是C++中的命名空间吗
- 在命名空间中使用全局命名空间中的函数
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- '使用命名空间{嵌套在另一个命名空间中的某个命名空间}"
- 是否可以将函数导入命名空间,但不能导出它?
- C++ C++类中的命名空间降级
- 如何使用 soong 命名空间来有条件地编译模块
- 使用 Clang++ 有没有办法将文件作为命名空间等包含?
- 使用 make 编译 MPI,几个命名空间错误,例如"错误:未知类型名称'使用'?
- 如何通过命名空间调用非静态方法
- 我如何在我的命名空间中使用LoadString宏