【问题标题】:Can't access parent class enum from outside无法从外部访问父类枚举
【发布时间】:2019-09-23 16:01:21
【问题描述】:

我想访问我的类的枚举,它继承自它的基类,但它给出了错误。

说我必须使用Base::One,而不是Extended::One

但是其他人不知道 Base 类,他们只知道我与他们一起发布的 Extended 类。

如何使用Extended::One 访问所有基类的枚举?

class Base {
    public:
    enum Type {
        One,
        Two
    };
};

class Extended : Base {

};

int main() {
    Extended::One; // ERROR: constant Base::One is inaccessible

    return 0;
}

【问题讨论】:

标签: c++ class enums


【解决方案1】:

您不小心使用了private inheritance。要解决此问题,请按如下方式定义 Extended

class Extended : public Base {

};

【讨论】:

  • 天哪,我看了 20 分钟才看到。谢谢!
【解决方案2】:

Type在Base中可能是public的,但是Base本身并不是Extended的公共基类,所以你的main函数并不知道。

如果其他类需要使用枚举,为什么不在全局范围内定义呢?

【讨论】:

    猜你喜欢
    • 2018-03-13
    • 2012-01-17
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    相关资源
    最近更新 更多