如果我的对象需要打印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>
或类似吗?
答案 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;
}