使用 stringstream 作为内联函数内的默认值
我正在学习 c++03,我刚刚开始接触 c++。
我想创建一个函数,在字符串流上设置一些属性并返回它(或者可能通过引用传递它),
inline stringstream get_fixed_stream(stringstream ss=stringstream("")) {
ss.precision(4);
ss.setf(ios::fixed);
return ss;
}
所以如果我调用:
stringstream ss = get_fixed_stream()
我接收一个新的字符串,如果我调用
cout = get_fixed_stream(cout)
精度和 setf都设置为cout。
我收到这个错误:
/usr/include/c++/4.4/streambuf:770: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]’ is private
/usr/include/c++/4.4/iosfwd:63: error: within this context
i'm on c++03 and i've just begin to approach c++.
i want to to make a function that set some properties on a string stream and return it (or mayebe passing it by reference)
inline stringstream get_fixed_stream(stringstream ss=stringstream("")) {
ss.precision(4);
ss.setf(ios::fixed);
return ss;
}
so if i call:
stringstream ss = get_fixed_stream()
i recive a new strinstring, if i call
cout = get_fixed_stream(cout)
the precision and setf are settet to cout.
i get this error:
/usr/include/c++/4.4/streambuf:770: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]’ is private
/usr/include/c++/4.4/iosfwd:63: error: within this context
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
std::cout
不是字符串流,不能作为一个字符串流传入。试试这个
std::cout
is not a stringstream and can't be passed in as one.try this instead
我建议使用参考(下面的&):
I suggest using a reference (the & below):