2017-12-03 2 views
0

std::basic_iostream<char>을 상속 한 스트림 클래스에 대해 스트림 추출 연산자를 구현하려고합니다. 불행히도 나는 정말로 이해하지 못하는 컴파일 오류를 얻는다.스트림 연산자를 구현할 때 컴파일 오류가 발생합니다.

이 내 단순화 (비 기능) 코드 : 내가 얻을 라인에서 작업자 및 하나를 구현하는 라인에 C2678 binary '>>': no operator found which takes a left-hand operand of type 'MyStream', 하나

#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

관련 문제