stringstream运算符选择问题
我有一个像这样的类构造函数:
DotDashLogMatcher( std::stringstream const& pattern );
我这样称呼它:
std::stringstream s;
DotDashLogMatcher( s << "test" );
这是一个过于简化的示例,但这本质上就是正在发生的事情。这是我收到的确切编译器错误。请注意,由于某种原因,传入的结果对象是 basic_ostream,我不确定这是否正常。它无法像我的函数所期望的那样将其转换为 std::stringstream 。
error C2664: 'DotDashLogMatcher::DotDashLogMatcher(const stlpd_std::stringstream &)' : cannot convert parameter 1 from 'stlpd_std::basic_ostream<_CharT,_Traits>' to 'const stlpd_std::stringstream &'
with
[
_CharT=char,
_Traits=stlpd_std::char_traits<char>
]
Reason: cannot convert from 'stlpd_std::basic_ostream<_CharT,_Traits>' to 'const stlpd_std::stringstream'
with
[
_CharT=char,
_Traits=stlpd_std::char_traits<char>
]
No constructor could take the source type, or constructor overload resolution was ambiguous
我在 Windows 上使用 VS2003 和 STLport。
有人知道我哪里出错了吗?为什么这段代码无法编译?如果我缺乏信息,我提前道歉。我将为那些需要更多信息的人更新我的问题。
I have a class constructor like so:
DotDashLogMatcher( std::stringstream const& pattern );
I call it like so:
std::stringstream s;
DotDashLogMatcher( s << "test" );
This is an over-simplified example, but that is essentially what is going on. Here is the exact compiler error I'm getting. Note that for some reason the resulting object that is passed in is a basic_ostream, I am not sure if this is normal. It isn't able to cast it to an std::stringstream like my function expects.
error C2664: 'DotDashLogMatcher::DotDashLogMatcher(const stlpd_std::stringstream &)' : cannot convert parameter 1 from 'stlpd_std::basic_ostream<_CharT,_Traits>' to 'const stlpd_std::stringstream &'
with
[
_CharT=char,
_Traits=stlpd_std::char_traits<char>
]
Reason: cannot convert from 'stlpd_std::basic_ostream<_CharT,_Traits>' to 'const stlpd_std::stringstream'
with
[
_CharT=char,
_Traits=stlpd_std::char_traits<char>
]
No constructor could take the source type, or constructor overload resolution was ambiguous
I'm using VS2003 and STLport on Windows.
Anyone know where I'm going wrong here? Why won't this code compile? I apologize in advance if I am lacking information. I will update my question for those that request more info.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
运算符<<不返回 std::stringstream,因为它是从 std::ostream 继承的。请参阅:
http://www.cplusplus.com/reference/iostream/stringstream/
您可以使用:
DotDashLogMatcher( s );
或者您可以更改方法声明以匹配返回类型。
operator<< does not return a std::stringstream because it is inherited from std::ostream. See:
http://www.cplusplus.com/reference/iostream/stringstream/
You may use:
DotDashLogMatcher( s );
Or you may change your method declaration in order to match the return type.
我相信您应该将该语句拆分为两个单独的命令:
因为参数是通过引用传递的,因此需要可修改,因此是左值。
I believe you should split the statement into two separate commands:
as the parameter is passed by reference and thus needs to be modifiable, therefore an l-value.
也许您想将: 更改
为:
问题是
operator <<
为 std::ostream 重载并返回std::ostream
。如果您无法更改它,有几种解决方法。
Maybe you want to change:
Into:
The problem is that
operator <<
is overloaded for std::ostream and returns astd::ostream
.If you can;t change it there are several workarounds.