Multiple inheritance. Ambiguous access errors

Hello, I have to implement multiple inheritance. But i met these errors: ambiguous access of 'a' and ambiguous access of 'b'. Where is my mistake?
Thanks!
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "stdafx.h"
#include "iostream"
using namespace std;

class BaseOne {

	protected:
		int a, b;

	public:

		int getMax(void)
		{
			if( a >= b && a >= b / a)
			{
				return a;
			}
			if( b >= a && b >= b / a)
			{
				return b;
			}
			if( b / a >= a && b / a >= b)
			{
				return b / a;
			}
		}
};

class BaseTwo {
	
	protected:
		int a, b;
	
	public:

		int getMin(void)
		{
			if( a <= b && a <= (double) (a / b))
			{
				return a;
			}
			if( b <= a && b <= (double) (a / b))
			{
				return b;
			}
			if( (double) (a / b) <= a && (double) (a / b) <= b)
			{
				return (double) (a / b);
			}
		}
};

class Derived: public BaseOne, public BaseTwo {
	
	public:

		void setA(int paramA)
		{
			a = paramA;
		}
		void setB(int paramB)
		{
			b = paramB;
		}
};

int main()
{
	Derived d;
	d.setA(5);
	d.setB(15);
	cout << "Max: " << d.getMax() << endl;
	cout << "Min: " << d.getMin() << endl;

	return 0;
}
You inherit four protected member variables:
1
2
3
4
BaseOne::a
BaseOne::b
BaseTwo::a
BaseTwo::b
You need to use the scope resolution operator as above to specify which you want. If there is only supposed to be one a and one b, you need to redesign.
And favour #include <iostream> over #include "iostream"
Last edited on
Topic archived. No new replies allowed.