C++, ambiguous inheritance error in vs 2010 - c++

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?

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.

Related

C++ unable to find method definition from base class [duplicate]

I have produced a minimal example to replicate the problem I am seeing with a more complex class hierarchy structure:
#include <string>
#include <iostream>
class A
{
protected:
virtual
~A() = 0;
};
inline
A::~A() {}
class B : public A
{
public:
virtual
~B()
{
}
std::string B_str;
};
class BB : public A
{
public:
virtual
~BB()
{
}
std::string BB_str;
};
class C : public A
{
protected:
virtual
~C()
{
}
virtual
void Print() const = 0;
};
class D : public B, public BB, public C
{
public:
virtual
~D()
{
}
};
class E : public C
{
public:
void Print() const
{
std::cout << "E" << std::endl;
}
};
class F : public E, public D
{
public:
void Print_Different() const
{
std::cout << "Different to E" << std::endl;
}
};
int main()
{
F f_inst;
return 0;
}
Compiling with g++ --std=c++11 main.cpp produces the error:
error: cannot declare variable ‘f_inst’ to be of abstract type ‘F’
F f_inst;
note: because the following virtual functions are pure within ‘F’:
class F : public E, public D
^
note: virtual void C::Print() const
void Print() const = 0;
^
So the compiler thinks that Print() is pure virtual.
But, I have specified what Print() should be in class E.
So, I've misunderstood some of the rules of inheritance.
What is my misunderstanding, and how can I correct this problem?
Note: It will compile if I remove the inheritance : public D from class F.
Currently your F is derived from C in two different ways. This means that an F object has two separate C bases, and so there are two instances of C::Print().
You only override the one coming via E currently.
To solve this you must take one of the following options:
Also override the one coming via D, either by implementing D::Print() or F::Print()
Make Print non-pure
Use virtual inheritance so that there is only a single C base.
For the latter option, the syntax adjustments would be:
class E : virtual public C
and
class D : public B, public BB, virtual public C
This means that D and E will both have the same C instance as their parent, and so the override E::Print() overrides the function for all classes 'downstream' of that C.
For more information , look up "diamond inheritance problem". See also Multiple inheritance FAQ

C++ A multiple inheritance pproblem with pure virtual functions

I have produced a minimal example to replicate the problem I am seeing with a more complex class hierarchy structure:
#include <string>
#include <iostream>
class A
{
protected:
virtual
~A() = 0;
};
inline
A::~A() {}
class B : public A
{
public:
virtual
~B()
{
}
std::string B_str;
};
class BB : public A
{
public:
virtual
~BB()
{
}
std::string BB_str;
};
class C : public A
{
protected:
virtual
~C()
{
}
virtual
void Print() const = 0;
};
class D : public B, public BB, public C
{
public:
virtual
~D()
{
}
};
class E : public C
{
public:
void Print() const
{
std::cout << "E" << std::endl;
}
};
class F : public E, public D
{
public:
void Print_Different() const
{
std::cout << "Different to E" << std::endl;
}
};
int main()
{
F f_inst;
return 0;
}
Compiling with g++ --std=c++11 main.cpp produces the error:
error: cannot declare variable ‘f_inst’ to be of abstract type ‘F’
F f_inst;
note: because the following virtual functions are pure within ‘F’:
class F : public E, public D
^
note: virtual void C::Print() const
void Print() const = 0;
^
So the compiler thinks that Print() is pure virtual.
But, I have specified what Print() should be in class E.
So, I've misunderstood some of the rules of inheritance.
What is my misunderstanding, and how can I correct this problem?
Note: It will compile if I remove the inheritance : public D from class F.
Currently your F is derived from C in two different ways. This means that an F object has two separate C bases, and so there are two instances of C::Print().
You only override the one coming via E currently.
To solve this you must take one of the following options:
Also override the one coming via D, either by implementing D::Print() or F::Print()
Make Print non-pure
Use virtual inheritance so that there is only a single C base.
For the latter option, the syntax adjustments would be:
class E : virtual public C
and
class D : public B, public BB, virtual public C
This means that D and E will both have the same C instance as their parent, and so the override E::Print() overrides the function for all classes 'downstream' of that C.
For more information , look up "diamond inheritance problem". See also Multiple inheritance FAQ

C++ multiple inheritance with base classes deriving from the same class

I have stumbled on a problem while trying to re-use code from different classes. I post it here in hope that some of you might be able to help me.
I have a set of classes (B,C) deriving from the same class (A) which forces the implementation of some methods (foo, run). Class B implements these method, and both B and C provide other methods:
#include<iostream>
template<class I, class O>
class A {
public:
A() {}
virtual ~A() {}
virtual void foo() const = 0; // force implementation of this function
virtual void run() const = 0; // force implementation of this function
};
template<class I, class O>
class B : public A<I,O> {
public:
B() {}
virtual ~B() {}
virtual void foo() const { // implementation for the Base class
std::cout << "B's implementation of foo" << std::endl;
}
virtual void run() const { // implementation for the Base class
std::cout << "B's implementation of run" << std::endl;
}
virtual void foobar() const { // some other function provided by this class
std::cout << "B's implementation of foobar" << std::endl;
}
};
template<class I, class O, class M>
class C : public A<I,O> {
public:
C() {}
virtual ~C() {}
virtual void bar(M m) const { // some other function provided by this class
std::cout << "C's implementation of bar with: " << m << std::endl;
}
};
Now, what I am trying to do is inherit from both B and C so that I can have the extra methods (foobar, bar), but also not have to implement the method from class A (foo) because it is already defined in B:
template<class I, class O>
class D : public B<I,O>, public C<I,O,int> {
public:
D() {}
void run() const {
this->bar(123);
this->foo();
this->foobar();
}
};
But for some reason the compiler gives me this error:
test.cpp: In function ‘int main(int, char**)’:
test.cpp:68:35: error: cannot allocate an object of abstract type ‘D<float, double>’
A<float, double> *d = new D<float, double>(); // what I need to do
test.cpp:48:11: note: because the following virtual functions are pure within ‘D<float, double>’:
class D : public B<I,O>, public C<I,O,int> {
^
test.cpp:9:22: note: void A<I, O>::foo() const [with I = float; O = double]
virtual void foo() const = 0; // force implementation of this function
This is the code I use to run it:
int main(int argc, char **argv)
{
A<float, double> *b = new B<float, double>();
b->foo(); // prints "B's implementation of foo"
b->run(); // prints "B's implementation of run"
//A<float, double> *c = new C<float, double, int>(); // obviously fails because C does not implement any of A's functions
//A<float, double> *d = new D<float, double>; // line 68: what I need to do
//d->run(); // ***throws the abstract class error
return 0;
}
I want to use the 'run' function of an object of class D from a pointer to a A. As all the functions are virtual I expect to execute implementation of each function defined in the lowest inheritance point, meaning that 'B::run' will be discarded. As 'D::run' uses functions from both B and C I need to inherit from both classes.
I hope I have described it enough and not confused anybody.
Thanks for the help!
If you change B and C to virtually inherit from the A template class, they will share a single base instance when combined by D and this error will go away:
template<class I, class O>
class B : virtual public A<I,O> {
// ...
template<class I, class O, class M>
class C : virtual public A<I,O> {
However, this pattern (known as the diamond inheritance (anti-)pattern) can be very difficult to reason about and I would strongly suggest avoiding it if possible. You are likely to run into even more obscure problems later.
Here is a sample of this technique working, but showing some results that may not be expected at first glance:
class A {
public:
virtual void foo() = 0;
};
class B : virtual public A {
public:
virtual void foo() override;
};
void B::foo()
{
std::cout << "B::foo()" << std::endl;
}
class C : virtual public A { };
class D : public B, public C { };
int main() {
D d;
C & c = d;
c.foo();
return 0;
}
Note that even though you are calling C::foo(), which is pure virtual, since there is only one A instance the inherited pure virtual function resolves to B::foo() though the shared A vtable. This is a somewhat surprising side-effect -- that you can wind up invoking methods implemented on a cousin type.
The answer by #cdhowie gives you a solution.
To understand the problem the compiler is complaining about, take a set of simpler classes:
struct A
{
virtual void foo() = 0;
};
struct B : A
{
virtual void foo() {}
}
struct C : A
{
void bar() {}
}
struct D : B, C
{
};
The class hierarchy of D is:
A A
| |
B C
\ /
D
With this inheritance structure, D has two virtual tables, one corresponding to the B inheritance hierarchy and one corresponding to C inheritance hierarchy. The difference being that in the B hierarchy, there is an implementation of A::foo() while there isn't one in the C hierarchy.
Let's say you were allowed to construct an object of type D.
D d;
C* cp = &d;
Now cp points to the C hierarchy of D, and uses a virtual table in which foo is not implemented. That will be a run time error that the compiler is helping you avoid at compile time.
I know this is a late answer but since you are deriving from a pure virtual function for class C, you have to implement it, then in those functions you call the base class:
virtual void foo() const { // for class C
B::foo();
}

c++ inheritance virtual functions

I am trying to learn inheritance in c++. I wrote some code to learn virtual functions.
#include <iostream>
using namespace std;
class A {
int a;
public:
A() {}
virtual int get_count() const = 0;
int get_A() { return a; }
};
class B : public A{
public:
int b;
B() {}
B(A& base)
: b(base.get_count()) {}
virtual int get_count() const { return 10; }
};
void func(A& base) {
B derived(base);
cout << derived.b;
}
int main() {
A base;
B derived;
func(derived);
}
When I try to compile I get this error:
test_inheritance_vir.cpp: In function ‘int main()’:
test_inheritance_vir.cpp:32: error: cannot declare variable ‘base’ to be of abstract type ‘A’
test_inheritance_vir.cpp:5: note: because the following virtual functions are pure within ‘A’:
test_inheritance_vir.cpp:10: note: virtual int A::get_count() const
Can you please tell me what I am doing wrong?
You are trying to instantiate an object of type A with A base;. It's not possible as A contains a pure virtual function. (get_count()) Suppose I tried calling base.get_count().
The method virtual int get_count() const = 0; is pure virtual. You can't create an object of a class that is abstract (or in other words - has a pure virtual member). If you want to create an object of A, remove the = 0 and define the function (with an empty body if you need):
virtual int get_count() const{};
should work.
The way you have implemented A (below) causes it to be an abstract base class.
class A
{
int a;
public:
A() {}
virtual int get_count() const = 0; // this is a pure virtual function
int get_A() { return a; }
};
It can only be used as a pointer to a derived class that implements the pure virtual functions:
int main()
{
B derived;
A* pA = new B; // this is okay
delete pA;
return 0;
}
The answers above are the technical reasons why it won't work, but there's a more insidious problem in that the design doesn't make sense. You're creating a B to add some functionality to your A. If you also create a C that extends A, do you really want to turn that into a B?
The classic example of inheritance is animals. Zebra and Giraffe are both Animals, so a class hierarchy would look like this
class Animal
{
stuff
}
class Zebra : public Animal
{
more stuff
}
class Giraffe : public Animal
{
different stuff
}
It doesn't make much sense to turn a Zebra into a Giraffe, though, even though both are animals.

C++ Weird Diamond inheritance issue

I have this
A
/ \
B C
\ /
D
A has a pure virtual function, prototyped as:
virtual A* clone(void) const = 0;
B and C virtually inherit from A ( class B: public virtual A, class C: public virtual A)
B has the virtual function, prototyped as:
virtual B* clone(void) const {};
C has the virtual function, prototyped as:
virtual C* clone(void) const {};
D inherits from both B & C like that: class D: public B, public C
D has the virtual function, prototyped as:
virtual D* clone(void) const {};
Now, when compiling I get the following 6 lines of errors:
error C2250: 'D' : ambiguous inheritance of 'B *A::clone(void) const'
No freaking idea how to solve this issue.
Thanks in advance.
Use virtual inheritance if you want only one copy of a parent in your hierarchy.
class B : public virtual A
Edit:
There may be a bug in MSVC++ 2010. The Intellisense doesn't detect a problem, but the compiler chokes on it. Strange since VC6 is happy enough with it.
As a workaround, if you declare D as follows, it makes MSVC++ 2010 happy while also working in compilers without this issue:
class D: public virtual A, public B, public C
What you describe in your original post is perfectly legal. A quick sample code that does exactly that compiles without any errors by Comeau Online compiler
class A {
public: virtual A* clone() const = 0;
};
class B: public virtual A {
public: virtual B* clone() const { return 0; }
};
class C: public virtual A {
public: virtual C* clone() const { return 0; }
};
class D: public B, public C
{
public: virtual D* clone() const { return 0; }
};
Either you are not doing what you said you are doing, or your compiler is broken. Post real code you are trying to compile.
P.S. I just tried compiling this in VS 2010 Express and got the same error. As Gunslinger47 also suggests in the comments, this is a bug in VS 2010 compiler.
avoid diamond inheritance? ;->
anyway, here is sample (really sample - don't cast like that)
// ConsoleCppTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
class A {
public:
virtual void* clone() = 0;
};
class B: public A {
public:
virtual void* clone() = 0;
};
class C: public A {
public:
virtual void* clone() = 0;
};
class D: public B, public C
{
public:
virtual void* B::clone()
{
std::cout << "B";
return (void*)this;
}
virtual void* C::clone()
{
std::cout << "C";
return (void*)this;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
D* d = new D();
void* b = ((B*)d)->clone();
void* c = ((C*)d)->clone();
return 0;
}