compile error - please help

Please help me find this compile error.
The code is a simplified Composition and Interfaces example from http://en.wikipedia.org/wiki/Composition_over_inheritance
I just posting the first few lines since the compile error is on line 9.

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
#include <iostream>
using namespace std;

// interface class
class Object
{
	public:
		Object(ADelegate *a, BDelegate *b) : _a(a), _b(b) { };
		void Afunc() { _a->Afunc(); };
		void Bfunc() { _b->Bfunc(); };
	private:
		ADelegate *_a;
		BDelegate *_b;
};

// delegate classes and abstract classes
class ADelegate
{
	public:
		virtual void Afunc()=0;
};
class A1 : public ADelegate
{
	public:
		virtual void Afunc() { cout << "A1 "; };
};


This is the output from the MinGW compiler:

D:\wolf\Documents\teensy\demo_MinGW>g++ polymorph_composition.cpp
polymorph_composition.cpp:9:20: error: expected ')' before '*' token
   Object(ADelegate *a, BDelegate *b) : _a(a), _b(b) { };
                    ^
polymorph_composition.cpp:13:3: error: 'ADelegate' does not name a type
   ADelegate *_a;
   ^
polymorph_composition.cpp:14:3: error: 'BDelegate' does not name a type
   BDelegate *_b;
   ^
polymorph_composition.cpp: In member function 'void Object::Afunc()':
polymorph_composition.cpp:10:18: error: '_a' was not declared in this scope
   void Afunc() { _a->Afunc(); };
                  ^
polymorph_composition.cpp: In member function 'void Object::Bfunc()':
polymorph_composition.cpp:11:18: error: '_b' was not declared in this scope
   void Bfunc() { _b->Bfunc(); };
                  ^

Thank you.
You need to forward declare ADelegate and BDelegate; you are using them before their definitions on lines 17 and 22. You'll also need to move the definitions of Afunc() and Bfunc() below the definitions of ADelegate and BDelegate since they refer to methods.
Thanks for identifying the problem Zhuge.
Fixed by moving the interface class below the delegate classes.
Here is the working 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
using namespace std;

// delegate classes and abstract classes
class ADelegate
{
	public:
		virtual void Afunc()=0;
};
class A1 : public ADelegate
{
	public:
		virtual void Afunc() { cout << "A1 "; };
};
class A2 : public ADelegate
{
	public:
		virtual void Afunc() { cout << "A2 "; };
};

class BDelegate
{
	public:
		virtual void Bfunc()=0;
};
class B1 : public BDelegate
{
	public:
		virtual void Bfunc() { cout << "B1 "; };
};
class B2 : public BDelegate
{
	public:
		virtual void Bfunc() { cout << "B2 "; };
};

// interface class
class Object
{
	public:
		Object(ADelegate *a, BDelegate *b) : _a(a), _b(b) { };
		void Afunc() { _a->Afunc(); };
		void Bfunc() { _b->Bfunc(); };
	private:
		ADelegate *_a;
		BDelegate *_b;
};

// concrete classes
class A1B1 : public Object
{
	public:
		A1B1 () :Object(new A1(), new B1()) { cout << "A1B1 "; };
};
class A2B1 : public Object
{
	public:
		A2B1 () :Object(new A2(), new B1()) { cout << "A2B1 "; };
};
class A1B2 : public Object
{
	public:
		A1B2 () :Object(new A1(), new B2()) { cout << "A1B2 "; };
};
class A2B2 : public Object
{
	public:
		A2B2 () :Object(new A2(), new B2()) { cout << "A2B2 "; };
};

int main()
{
	A1B1 objA1B1;
	objA1B1.Afunc();
	objA1B1.Bfunc();
	cout << endl;

	A2B1 objA2B1;
	objA2B1.Afunc();
	objA2B1.Bfunc();
	cout << endl;

	A1B2 objA1B2;
	objA1B2.Afunc();
	objA1B2.Bfunc();
	cout << endl;

	A2B2 objA2B2;
	objA2B2.Afunc();
	objA2B2.Bfunc();
	cout << endl;

	return 0;
}


output:
A1B1 A1 B1
A2B1 A2 B1
A1B2 A1 B2
A2B2 A2 B2
Topic archived. No new replies allowed.