5

I have a case in project where I need to inherit an abstract class virtually into two different classes which are again inherited into final class but the compiler keeps showing me this error

 error C2250: 'D': ambiguous inheritance of 'B &A::doit(void)'

How do I remove such error , class sample is as follow

class A {
public:
    virtual A& doit() = 0;
};

class B :virtual public A {
public:
    virtual B& doit() {
        cout << "B";
        return *this;
    }
};
class C : virtual public A {
public:
    C& doit() {
        cout << "C";
        return *this;
    }
};
class D : public B, public C {
public:
    D& doit() {
        cout << "D";
        return *this;
    }
};

I have tried deleting base class functions first but it does not seem to work.

  • If you can return `A&` from all `doit()`s (i.e. avoid covariant return), it will solve the issue. – wohlstad Aug 17 '22 at 08:40
  • @wohlstad I need to use covariant return too at times – user19783152 Aug 17 '22 at 08:44
  • 1
    You seem to be using MSVC. It used to be a known bug long time ago :https://stackoverflow.com/questions/6933061/c-ambiguous-inheritance-error-in-vs-2010. Maybe it's still there. BTW - gcc accepts your code, and so is clang. – wohlstad Aug 17 '22 at 08:50
  • What's your compiler and the compiler flags you're using? I tried it with C++20 and calling `doit` from a `D` object and it works fine? – Nathan Furnal Aug 17 '22 at 08:51
  • Checking with C++20 - live - https://godbolt.org/z/orqn769od as @wohlstad says just MSVC fails to compile this clang and GCC compile ok. – Richard Critten Aug 17 '22 at 08:54
  • @NathanFurnal Its msvc , in visual studio, looking at project settings , version of compiler seems to be iso c++14 standard – user19783152 Aug 17 '22 at 08:56
  • @user19783152 Others have been faster, it's an MSVC problem. – Nathan Furnal Aug 17 '22 at 08:59

0 Answers0