Classes that access each other

I'm having some sort of forward declaration problem.

I have 3 classes, A, B, C

class B has pure virtual functions and class C is a child of B

This is what I want: I want class A to be able to make instances of C, and to be able to call functions in C, and I want C to be able to access functions of A. There will only be one instance of A, but there will be many instances of C.

This is what I have:

A.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "C.h"

class B;
class A
{
	public:
		
		A();
		~A();
		bool foo();
		bool foo2();

	private:
		B * board[8][8];	
};


B.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class A;
class B
{
	
	public:
	
		B(A * obj) : ptrtoA(ptr)
		{
		}

		virtual ~B();

		virtual void bar() = 0;

	protected:

		A * ptrtoA;	
};


C.h:
1
2
3
4
5
6
7
8
9
10
11
12
#include "B.h"

class C : public B
{
	public:
		C(A * ptr) : B(ptr) //calls super constructor
                {
                }  

		void bar();

};


So here's where I have problems.

Currently if I do this inside foo():

1
2
board[0][0] = new C(this);
board[0][0]->bar();


Then it compiles fine, but if I put this inside of bar():

 
board->foo2();


(where board is of type A *)
I get:
1
2
model/src/C.cpp:28:7: error: invalid use of incomplete type ‘struct A’
model/inc/B.h:13:7: error: forward declaration of ‘struct A’



All I want is for A to be able to access children of B and vice versa! Tips?
Last edited on
Include A.h in C.cpp
You could use this: http://www.cplusplus.com/doc/tutorial/inheritance/

Remember: friend's are your friends.
@Peter87 - that was it - thanks so much!

@Ben - I was thinking of doing that, but will children of a friended class still be able to access the friend, or would I have to friend each child as well?

I.e. if I put friend class B; into class A, would children of class B inherit the friendship?
Last edited on
Topic archived. No new replies allowed.