#include <iostream>
using namespace std;

class X {
public:
	X(int)
	{
		cout << "X()" << endl;
	}
	void f()
	{
		cout << "f()" << endl;
	}
};

class Y: public X {
public:
	Y(int): X(1)
	{
		cout << "Y()" << endl;
	}
};

class Z: public X {
public:
	Z(int): X(1)
	{
		cout << "Z()" << endl;
	}
};

class A: public Y, public Z {
public:
	A(int): Y(1), Z(2) {}
	using Y::f;
};

int main()
{
	A a(2);
	a.f();
}

Why when I call f() in main() i get this error. What is wrong ?

error: ‘X’ is an ambiguous base of ‘A’

worked fine on my pc.
gave this output:
X()
Y()
Z()
f()
press any key to continue.

I am using Visual studio 2008,windows

worked fine on my pc.
gave this output:
X()
Y()
Z()
f()
press any key to continue.

I am using Visual studio 2008,windows

When Multiple inheritance isn't implemented properly, what happens is highly compiler specific. Users of VC++ are the lucky ones, it will APPEAR to work correctly.

To implement Multiple Inheritance properly, virtual inheritance is required.

Once you do that, X will be considered to be inherited directly by A instead of through Y and Z, so a minor change to your A constructor will be necessary:

A(int): Y(1), Z(2)[B], X(3)[/B] {}

I am using Eclipse and Mingw in Linux,Fedora....I am confuse

Thanks Fbody,didnt saw your post

When Multiple inheritance isn't implemented properly, what happens is highly compiler specific. Users of VC++ are the lucky ones, it will APPEAR to work correctly.

To implement Multiple Inheritance properly, virtual inheritance is required.

Once you do that, X will be considered to be inherited directly by A instead of through Y and Z, so a minor change to your A constructor will be necessary:

A(int): Y(1), Z(2)[B], X(3)[/B] {}

Ya,I guess you are right!...you have to explicitly mention that X.

My respect for Visual studio has increased after reading your comment...:)

This implementation is called the "dreaded diamond", this faq is very explanatory.

@arshad:>>My respect for Visual studio has increased after reading your comment
@Fbody:>>Users of VC++ are the lucky ones

MY respect for Visual studio has _decreased_ (even more) since I saw your post. I tried to find a part of the C++ standard that prescribes the behaviour of the code by the OP but to no avail. However, from me and several sources on the internet, there is a consensus that what the OP reported is what should indeed happen. If no virtual inheritance is mandated by the programmer, each inheritance path should be followed separately, leading to two instances of the base class (which can lead to ambiguous uses or references to the base class members). The fact that Visual Studio's compiler inserts a virtual inheritance scheme where none was explicitely mandated by the programmer, is a very bad thing (what other dirty hidden tricks does it do?). Although I couldn't find a passage that says that this violates the standard, I would suspect it does, and in my opinion, it does violate the strict syntax philosophy of C++. In general, the GNU GCC compiler is much more strictly compliant with the standard (i.e. if there is a difference in behavior between VS and GCC, then GCC's output can be taken as the standard expected behavior).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.