Silverspur Silverspur - 3 years ago 380
C++ Question

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

Below is the class hierarchy I have.


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
where an
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
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.

Answer Source

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

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download