2017-06-06 8 views
1

継承されたクラスの演算子を< <にオーバーロードしましたが、正常に動作していますが、演算子>>をオーバーロードしようとすると、多くのエラーが発生します。 私の間違いは何ですか?Overload >> in inherited class C++

class Base{ 
private: 
    virtual std::ostream& print(std::ostream&) const = 0; 
    virtual std::istream& read(std::istream&); 
protected: 
    //atributes 
public: 
    //other functions 
    friend std::ostream& operator << (std::ostream& os, const Base& b) { 
     return b.print(os); 
    } 
    friend std::istream& operator >> (std::istream& is, Base& bb) { 
     return bb.read(is); 
    } 
}; 

class Inherited: public Base{ 
private: 
    //atributes 
    std::ostream& print(std::ostream& os) const { 
     //things I want to print 
    } 
    std::istream& read(std::istream& is){ 
     //things I want to read 
     return is; 
    } 
public: 
    //other functions 
}; 

istreamをvirtual pure(virtual ... const = 0;)として定義しても動作しません。

+0

[MCVE](https://stackoverflow.com/help/mcve)を提供してください。このMCVEで、コンパイラからエラーのテキストと正確なテキストをコンパイルしようとします。 – yeputons

答えて

1

あなたはのBasereadを純粋に宣言していません。

virtual std::istream& read(std::istream&); 

上記の宣言では、コンパイラ/リンカーは、基本クラスの関数の実装を想定しています。問題を解決するには、機能を純粋なvirtualの関数Baseにします。

virtual std::istream& read(std::istream&) = 0; 

PSがconstメンバ関数ではないことを注意。

関連する問題