【发布时间】:2021-03-01 23:26:59
【问题描述】:
我不断收到此错误消息:
状态错误 C2664 -- int MessageBoxW(HWND,LPCWSTR,LPCWSTR,UINT)': 无法将参数 2 从 'const char *' 转换为 'LPCWSTR' " 31
这是我下面的代码。我知道这与通过错误类中的 what() 函数传递 const 类型有关。由于某种原因,它不兼容。有什么想法吗?
// on startup execute to determine exceptions
try
{
// instantiate object app of class AppWindow
AppWindow app;
// if(initialize function in class app is executed, and while the app is running, broadcast app)
if (app.init())
{
while (app.isRun())
{
app.broadcast();
}
}
}
// if the following error is found, execute MessageBox function with following parameters
catch (const std::runtime_error& error)
{
// parameters(has no owner window so displays independently)
MessageBox(nullptr, error.what(), L"An error has occured", MB_OK);
}
return 0;
【问题讨论】:
-
一个
LPCWSTR是一个指向wchar_t元素数组的指针,每个元素都是16 位而不是8 位字符。 -
stackoverflow.com/questions/17643122/messageboxw-cannot-convert 和 stackoverflow.com/questions/35726799/… 仅举两个例子,通过将您的错误消息直接粘贴到本网站的搜索字段中获得。在对窄字符串进行消息装箱时,硬使用 MessageBoxA。