【发布时间】: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<std::wstring> 或类似的吗?
【问题讨论】: