简体   繁体   English

串流运算符选择问题

[英]stringstream operator selection issues

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. 请注意,由于某种原因,传入的结果对象是basic_ostream,我不确定这是否正常。 It isn't able to cast it to an std::stringstream like my function expects. 它无法像我的函数期望的那样将其强制转换为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

I'm using VS2003 and STLport on Windows. 我在Windows上使用VS2003和STLport。

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. 对于需要更多信息的人,我将更新我的问题。

operator<< does not return a std::stringstream because it is inherited from std::ostream. operator <<不返回std :: stringstream,因为它是从std :: ostream继承的。 See: 看到:

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

You may use: 您可以使用:

DotDashLogMatcher( s ); 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: 我相信您应该将语句分为两个单独的命令:

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

as the parameter is passed by reference and thus needs to be modifiable, therefore an l-value. 因为参数是通过引用传递的,因此需要进行修改,因此为l值。

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 . 问题是operator <<对于std :: ostream重载并返回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") );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM