C++, ambiguous inheritance error in vs 2010
Asked Answered
P

1

10

I have some troubles with the application of polymorphism in this example. This question is similar to my last question

C++, virtual inheritance, strange abstract class + clone problem

There are 3 abstract classes:

class A
{
public:
    virtual A  * copy () const = 0;
    virtual ~A() = 0;
};

A::~A(){}

class B
{
public:
    virtual B  * copy () const = 0;
    virtual ~B() = 0;
};

B::~B(){}

class C: virtual public A , public B 
{
public:
    virtual C  * copy () const = 0;
    virtual ~C() = 0;
};

C::~C(){}

and two inherited classes using the virtual inheritance

class D: virtual public A
{
public:
    virtual D  * copy () const {return new D  (*this);}
    virtual ~D() {}
};

class E: virtual public D , public C
{
public:
    virtual E * copy () const {return new E (*this);}
    virtual ~E() {}
}; //Error C2250: 'E' : ambiguous inheritance of 'D *A::copy(void) const

The above mentioned error occurs only using MSVS 2010 compiler, g++ compiles this code OK.

Class diagram (simplified)

.......... A .... B.....
........../.\..../......
........./...\../.......
......../.....\/........
.......D...... C........
........\...../.........
.........\.../..........
..........\./...........
...........E............

Last discussion we close with the result: remove the declaration of the copy() method from class C.

class C: virtual public A , public B 
{
public:
    //virtual C  * copy () const = 0; //remove declaration
    virtual ~C() = 0;
};

C::~C(){}

My sample code using polymorphism needs to create vector of pointers to C. After removing some element I want to create its copy... I NEED a declaration of copy() in class C, so removal of the declaration is insufficient and it does not solve the problem.

int main(int argc, char* argv[])
{
std::vector <C*> items;
items.push_back(new E());
items.push_back(new E());
items[0]->copy();
return 0;
}

Could you help me, please how to correct the code to be translatable using VS 2010?

Procyon answered 3/8, 2011 at 20:44 Comment(2)
Look up virtual inheritance in your favorite C++ book, FAQ, or reference site. Also search the web for "C++ dreaded diamond inheritance".Labrador
I know this problem... But how does it how relate to my question? This code is correct, but MSVS 2010 does not compile it however g++ ant oher compilers yes...Procyon
V
10

This is a known bug in Visual C++:

Visual C++ incorrectly reports ambiguity when covariance is used with virtual inheritance

You either need to eliminate the covariance or the virtual inheritance. Unfortunately, you can't have both.

Velites answered 3/8, 2011 at 20:53 Comment(3)
Thanks for your answer. If I understand correctly, so the error will corrected in the next a release VS... Is there any other way how to compile the code using MSVS2010 (compiler settings...)?Procyon
No, the bug was closed as "Won't Fix" meaning it won't be fixed in the next release. It appears that it is not possible to use Visual C++ to compile code that combines virtual inheritance with covariance. If this is an important issue for you, please go to that Connect bug, upvote it, click the "I can too" button under "N users can repro this bug," and leave a comment.Velites
It's annoying. Could we expect that this problem will ever be sometimes fixed? I do not have any experience with bug reporting to Microsoft...Procyon

© 2022 - 2024 — McMap. All rights reserved.