gpt4 book ai didi

c++ - stringstream 运算符选择问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:15:38 25 4
gpt4 key购买 nike

我有一个这样的类构造器:

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。

有人知道我这里哪里出错了吗?为什么这段代码不能编译?如果我缺少信息,我会提前道歉。对于那些需要更多信息的人,我会更新我的问题。

最佳答案

operator<< 不返回 std::stringstream,因为它继承自 std::ostream。见:

http://www.cplusplus.com/reference/iostream/stringstream/

您可以使用:

DotDashLogMatcher( s );

或者您可以更改方法声明以匹配返回类型。

关于c++ - stringstream 运算符选择问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7814305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com