stringstream运算符选择问题

发布于 2024-12-11 02:39:54 字数 1135 浏览 1 评论 0原文

我有一个像这样的类构造函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陪你搞怪i 2024-12-18 02:39:54

运算符<<不返回 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.

李不 2024-12-18 02:39:54

我相信您应该将该语句拆分为两个单独的命令:

s << "test";
DotDashLogMatcher( s );

因为参数是通过引用传递的,因此需要可修改,因此是左值。

I believe you should split the statement into two separate commands:

s << "test";
DotDashLogMatcher( s );

as the parameter is passed by reference and thus needs to be modifiable, therefore an l-value.

丑疤怪 2024-12-18 02:39:54

也许您想将: 更改

DotDashLogMatcher( std::stringstream const& pattern );

为:

DotDashLogMatcher( std::ostream const& pattern );

问题是 operator << 为 std::ostream 重载并返回 std::ostream

如果您无法更改它,有几种解决方法。

std::stringstream s;
s << "test"
DotDashLogMatcher( s );

// slightly more dangerious but should work.
std::stringstream s;
DotDashLogMatcher( static_cast<std::stringstream const&>(s << "test") );

Maybe you want to change:

DotDashLogMatcher( std::stringstream const& pattern );

Into:

DotDashLogMatcher( std::ostream const& pattern );

The problem is that operator << is overloaded for std::ostream and returns a std::ostream.

If you can;t change it there are several workarounds.

std::stringstream s;
s << "test"
DotDashLogMatcher( s );

// slightly more dangerious but should work.
std::stringstream s;
DotDashLogMatcher( static_cast<std::stringstream const&>(s << "test") );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文