1

私は次のようなクラスコンストラクタを持っています:

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を使用しています。

ここで私が間違っている場所を知っている人はいますか?このコードがコンパイルされないのはなぜですか? 情報が不足している場合は、あらかじめお詫び申し上げます。詳細情報を要求する人のために、質問を更新します。

4

3 に答える 3

0

多分あなたは変えたいです:

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") );
于 2011-10-18T23:38:59.653 に答える