【问题标题】:Overload >> in inherited class c++重载 >> 在继承的类 C++
【发布时间】:2017-06-06 23:40:10
【问题描述】:

我在继承的类中重载了运算符 > 时,它会显示很多错误。 我的错误是什么?

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;) 也不起作用。

【问题讨论】:

  • 请提供MCVE,我们可以尝试在该 MCVE 上编译来自您的编译器的错误的准确文本。

标签: c++ class inheritance operator-overloading istream


【解决方案1】:

您没有在Base 中声明read 是纯virtual

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

通过上述声明,编译器/链接器期望在基类中实现该函数。要解决此问题,请将函数设为 Base 的纯 virtual 函数。

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

PS请注意,它不是const 成员函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-05
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2015-08-04
    • 1970-01-01
    相关资源
    最近更新 更多