Search code examples
c++inheritancemultiple-inheritancevirtual-inheritance

"X is an ambiguous base of Y": Multiple inheritance nightmare


Below is the class hierarchy I have.

All Ixxxx classes are interfaces (abstract classes with no member data).

All arrows represent inheritance.

Colors are only here to provide better visualisation.

class hierarchy

Somewhere in the code using these classes, I have to pass a Track* where an IConnectableTrack* is expected, and I get the following compilation error:

error: ‘IConnectableTrack’ is an ambiguous base of ‘Track’

I know this is a matter of multiple inheritance, but I tried multiple combinations of virtual inheritances to no avail.

In particular, I thought virtualizing both inheritances between red interfaces and both inheritances between green classes (i.e. all purple arrows) would be enough, but it does not solve the problem.

What would be the correct construct here?

Edit: this question is different from the mentionned one since it includes pure abstract classes, which relates to the specific error message referenced.


Solution

  • Your Track has four instances of IConnectableTrack and compiler doesn't know which one to use. Make top 3 arrows virtual.

    https://wandbox.org/permlink/Y1e9LwDI0IVaERaW

    class IConnectableTrack {};
    class Object {};
    class ConnectableTrack: public virtual IConnectableTrack, public Object {};
    class IOccupiableTrack: public virtual IConnectableTrack {};
    class OccupiableTrack: public IOccupiableTrack {};
    class ISignalledTrack: public virtual IConnectableTrack {};
    class SignalledTrack: public ISignalledTrack {};
    class Track: public OccupiableTrack, public SignalledTrack {};
    
    void f(IConnectableTrack&) {}
    
    int main() {
        Track t;
        f(t);
    }