0

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

#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如果我通过 a ,我如何打印 s 和其他原始数据类型wcerr?我需要boost::lexical_cast<std::wstring>或类似的吗?

4

1 回答 1

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;
}
于 2012-08-28T18:05:32.580 回答