我想使用以下代码:
wstring s = L"GetLockCount " + g_SpeakerAlias.c_str();
MessageBoxW(NULL, unsignedinttowstring(iLockCount).c_str(), s, MB_OK);编译器告诉我:
The expression must have an integer enumeration type or an enumeration type without range restriction 我尝试连接两个wstring的方式似乎是错误的:
L"GetLockCount " + g_SpeakerAlias.c_str()声明如下:
wstring g_SpeakerAlias=L"SomeName";发布于 2021-08-20 19:58:19
.c_str()不是连接所必需的。您只需要在MessageBoxW中使用它,因为它需要LPCWSTR。
这是正确的:
wstring s = L"GetLockCount " + g_SpeakerAlias;
MessageBoxW(NULL, unsignedinttowstring(iLockCount).c_str(), s.c_str(), MB_OK);需要明确的是,这样做的原因是,虽然L"GetLockCount“不是wstring (它是宽字符的C样式字符串文字,它不是C++ wstring),但g_SpeakerAlias是wstring,并且operator+存在重载,其中左侧是wchar_t*,右侧是生成另一个wstring的wstring。当您调用.c_str()时,它会失败,因为没有覆盖wchar_t* + wchar_t*的重载。另一种解决方案是执行wstring s= L"GetLockCount ";,然后从文字开始使用s += g_SpeakerAlias;;wstring构造,然后连接。
https://stackoverflow.com/questions/68864507
复制相似问题