首页 > 解决方案 > 如何将 OutputDebugString 与 std::string 一起使用?

问题描述

我正在尝试做:

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

它不会编译,说它不能转换const char*LPCWSTR. 有没有办法输出std::string到调试窗口?

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

标签: c++

解决方案


您的项目配置为针对 Unicode 进行编译,因此OutputDebugString()映射到OutputDebugStringW(),它需要 aconst wchar_t*作为输入,而不是 a const char*,因此会出现错误。

视频中的代码有效,因为演示者的项目配置为针对 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() );
    

推荐阅读