plz help me (polymorphism in c++)

why this error happen?

http://f.cl.ly/items/0o0k2g3T3y3v3v2z232F/polymorphism.png

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;
class base
{
public:
	int i;
	base (int x)
	{
		i=x;
	}
};
class derived1:virtual public base
{
public:
	int j;
	derived1(int x,int y):base(x)
	{
		j=y;
	}
};
class derived2:virtual public base
{
public:
	int k;
	derived2(int x,int z):base(x)
	{
		k=z;
	}
};
class derived3:public derived1,public derived2
{
public:
	derived3(int x,int y,int z):derived1(x,y):derived2(x,z);
};
int main()
{
	derived3 ob(10,3,5);
}
Last edited on
Because that's not how an initializer list looks. It needs to be like this:
derived3(int x,int y,int z) : derived1(x,y), derived2(x,z) {}

Also, why are you inheriting with "virtual public"? You can only inherit with private, protected, and public - I'm not sure what virtual public is...
http://en.wikipedia.org/wiki/Virtual_inheritance
derived3(int x,int y,int z): base(42), derived1(x,y), derived2(x,z){/**/}
thanks for the link ne555 I was wondering about that also. strangely enough not many of the books i have discuss the diamond problem.
tanx ne555,
but why use "base (42)"?
i have many question but i can't write and speak english very well.
Last edited on
You need to assign a proper value to the member i. That it is done using the base constructor.
¿who is responsible to call that constructor?
Topic archived. No new replies allowed.