conversion to inaccessible base class is not allowed

24,319
class D1 :B1

Inheritance of classes is private by default, you should make it public:

class D1 : public B1
Share:
24,319
Cheng Lu
Author by

Cheng Lu

Updated on November 02, 2020

Comments

  • Cheng Lu
    Cheng Lu over 3 years

    I define a class B1 and a derived class D1 at first. Then I want to define a reference to B1 and initialize that to the D1 object I just defined. Here comes the error, saying that "conversion to inaccessible base class 'B1' is not allowed", which I don't know why.

    #include "std_lib_facilities.h"
    
    class B1 {
    public:
        virtual void vf() { cout << "B1::vf()\n"; };
        void f() { cout << "B1::f()\n"; };
    };
    
    class D1 :B1 {
    public:
        void vf() { cout << "D1:vf()\n"; };
        void f() { cout << "D1::f()\n"; };
    };
    
    
    
    
    int main()
    {
        B1 b1;
        b1.vf();
        b1.f();
    
        D1 d1;
        d1.vf();
        d1.f();
    
        B1& db=d1;//error
    
        keep_window_open();
        return 0;
    }
    
    • Igor Tandetnik
      Igor Tandetnik over 6 years
      Did you mean to make B1 a private base? If not, make it class D1 : public B1 {...}; If yes, then the error message is proper and expected - the whole point of private inheritance is to make the base class inaccessible.