2017-12-03 7 views
0

std::basic_iostream<char>を継承するストリームクラスのストリーム抽出演算子を実装しようとしています。 残念ながら私は本当に理解できないコンパイルエラーを受けます。ストリーム演算子を実装するときにコンパイルエラー

これは私の簡素化(非機能)コードです: C2678 binary '>>': no operator found which takes a left-hand operand of type 'MyStream'、私は私が得るラインにオペレータと1を実装ラインでの1:私は2つの類似したエラーを取得しています

#include <iostream> 

class MyWhateverClass { 
public: 
    int bla; 
    char blup; 
}; 

class MyBuffer : public std::basic_streambuf<char> { 
}; 

class MyStream : public std::basic_iostream<char> { 
    MyBuffer myBuffer; 
public: 
    MyStream() : std::basic_iostream<char>(&myBuffer) {} 

    std::basic_iostream<char>& operator >> (MyWhateverClass& val) { 
     *this >> val.bla; 
     *this >> val.blup; 
     return *this; 
    } 
}; 

int main() 
{ 
    MyStream s; 
    s << 1; 
    int i; 
    s >> i; 

    return 0; 
} 

ストリームからのint

面白い詳細は、オペレータの実装を削除すると両方のエラーが消えてしまうことです。

ここで何が起こっているのか誰にでも教えてください。

答えて

1

私はこの問題を解決しました。コンパイルエラーが発生する理由はシャドーイングです。あなたのMyStream::operator>>(MyWhateverClass&)は、std::basic_iostream::operator>>のすべてのバージョンを陰にします。この問題を解決するには、using declarationを使用する必要があります。

class MyStream : public std::basic_iostream<char> { 
    MyBuffer myBuffer; 
public: 
    MyStream() : std::basic_iostream<char>(&myBuffer) {} 

    using std::basic_iostream<char>::operator>>; 
    std::basic_iostream<char>& operator >> (MyWhateverClass& val) { 
     *this >> val.bla; 
     *this >> val.blup; 
     return *this; 
    } 
}; 

P.S.最初の答えは完全に間違っていたので、それを保存する必要はありません)

+0

恐ろしいですが、これを修正しました:)このコンテキストでは、「使用しています」は新しくありません。 – user2328447

関連する問題