【问题标题】:Error C2664: MessageBoxW cannot convert argument 2 from 'const char ' to 'LPCWSTR'错误 C2664:MessageBoxW 无法将参数 2 从“const char”转换为“LPCWSTR”
【发布时间】: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;

【问题讨论】:

标签: c++ window


【解决方案1】:

std::runtime_error::what() 返回const char*,所以你应该使用MessageBoxA(),而不是MessageBox()MessageBoxW()

MessageBoxA(nullptr,  error.what(), "An error has occured", MB_OK);

另外不要忘记从字符串文字中删除 L 前缀。

【讨论】:

    【解决方案2】:

    您可以使用MessageBoxA() 并从标题字符串中删除L 前缀。或者将e.what() 转换为wchar_t 字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 2011-07-25
      • 1970-01-01
      相关资源
      最近更新 更多