如何将int打印到std :: wcerr?

时间:2012-08-28 18:01:12

标签: outputstream widestring

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

#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;
}

换句话说:如果我通过int,我如何打印wcerr和其他原始数据类型?我需要boost::lexical_cast<std::wstring>或类似吗?

1 个答案:

答案 0 :(得分: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;
}