如何使用带有std :: string的OutputDebugString?

时间:2018-08-02 00:05:03

标签: c++

我正在尝试:

std::string line = "bla";
OutputDebugString( line.c_str() );

它无法编译,无法将const char*转换为LPCWSTR。没办法将std::string输出到调试窗口吗?

我也不明白为什么在本教程视频中,此方法似乎起作用:https://youtu.be/EIzkeFTpMq0?list=PLqCJpWy5Fohfil0gvjzgdV4h29R9kDKtZ&t=2101

1 个答案:

答案 0 :(得分:5)

您的项目已配置为针对Unicode进行编译,因此OutputDebugString()映射到OutputDebugStringW(),后者期望将const wchar_t*作为输入,而不是const char*,因此会出现错误。 / p>

视频中的代码有效,因为演示者的项目已配置为针对ANSI进行编译,因此OutputDebugString()映射到OutputDebugStringA()

因此,您需要:

  • 使用std::wstring代替std::string

    std::wstring line = L"bla";
    OutputDebugString( line.c_str() );
    
  • 使用OutputDebugStringA()代替OutputDebugString()

    std::string line = "bla";
    OutputDebugStringA( line.c_str() );
    
相关问题