문제

How do I write the output operator<< if my object needs to print a std::wstring as well as ints, etc?

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

In other words: How can I print ints and other primitive data types if I am passed a wcerr? Do I need boost::lexical_cast<std::wstring> or similar?

도움이 되었습니까?

해결책

#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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top