2010-02-03 50 views
4

我可以做的std :: ostringstream愁楚

std::ostringstream oss; 
oss << 1; 
oss.str(); 

所以为什么我不能做:

((std::ostringstream()) << 1).str() ? 

谢谢!

+0

错误说的是什么? – 2010-02-03 22:56:08

+0

你究竟想要完成什么? – 2010-02-03 22:58:00

+3

另请参见:http://stackoverflow.com/questions/1540831/stringstream-temporary-ostream-return-problem与使用临时stringstreams有趣的问题。 – 2010-02-03 23:01:22

回答

11

<<运算符返回基类型ostream,而只存在所导出的类型ostringstreamstr成员函数。

+1

(oss << 1).str()以同样的方式失败。 ostream不支持str() – pm100 2010-02-03 23:03:42

+0

你可以将它重新转换为ostringstream吗? – Inverse 2010-02-04 06:02:30

+0

是的,将它重新投放会有效。 – interjay 2010-02-04 14:47:59