【问题标题】:How to print int to std::wcerr?如何将 int 打印到 std::wcerr?
【发布时间】:2012-08-28 18:01:12
【问题描述】:

如果我的对象需要打印std::wstring 以及整数等,我该如何编写输出operator<<?

#include <iostream>

struct Foo {
  int i;
  std::wstring wstr;
};

std::ostream& operator<<(std::ostream& out, Foo const& foo) {
  out << foo.i << foo.wstr; // error                                                                                            
  return out;
}

int main() {
  Foo foo;
  std::wcerr << foo << std::endl;
}

换句话说:如果我传递了wcerr,我如何打印ints 和其他原始数据类型?我需要boost::lexical_cast&lt;std::wstring&gt; 或类似的吗?

【问题讨论】:

    标签: outputstream widestring


    【解决方案1】:
    #include <iostream>
    
    struct Foo {
       int i;
       std::wstring wstr;
    };
    
    std::wostream& operator<<(std::wostream& out, Foo const& foo) {
       out << foo.i << foo.wstr;                                                                                             
       return out;
    }
    
    int main() {
       Foo foo;
       std::wcerr << foo << std::endl;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-23
      • 2014-03-03
      • 1970-01-01
      • 2023-03-17
      • 2023-03-12
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多