circular dependence problem?

I'm having a frustrating time trying to get this to work:
I have 4 Classes: A, B, X, Y.

1
2
3
4
5
class A
{
...
   vector< vector <B> > chessboard;
}

1
2
3
4
5
6
class X;
class B
{
...
   X * ptr;
}

1
2
3
4
5
6
7
8
9
10
class B;
class X
{
public:
   X(vector< vector <B> > * board) : chessboard(board)
   virtual void doSomething() = 0;

protected:
   vector< vector <B> > * chessboard;
}

1
2
3
4
5
6
class Y : public X
{
public:
   Y(vector< vector <B> > * board) : X(board)
   void doSomething();
}


If I do this, no problem:
1
2
3
//code in class A
chessboard[0][0].setPiece(new Y(&chessboard));
cout << (chessboard[0][0].getX())->doSomething()


If I add this though:
1
2
//code in class Y, doSomething()
(*chessboard)[0][0].doSomethingElse();


The compiler explodes:

error: invalid use of incomplete type ‘struct B’
model/inc/X.h:13:7: error: forward declaration of ‘struct B’



All I want is for A and Y to be able to see and access the same chessboard! But the problem is that the board is a vector array of type B and B contains a pointer to X, which is the parent of Y. What am I missing?
Last edited on
Put the member function definitions in source files and include the headers that is needed in them.
Ok, I should clarify:

Classes A, B, X, and Y, are all defined in A.h, B.h, X.h, and Y.h

All of the code for the functions is defined in A.cpp, B.cpp, etc.
ok sounds good. specify the base class constructor when you define the constructor and not in the declaration. You have done this for both X and Y constructor.
1
2
3
4
5
6
class X;
class B
{
...
   X * ptr;
}


And
1
2
3
4
5
6
7
8
9
10
class B;
class X
{
public:
   X(vector< vector <B> > * board) : chessboard(board)
   virtual void doSomething() = 0;

protected:
   vector< vector <B> > * chessboard;
}


cannot be declared in two ways in declared before one another .
Topic archived. No new replies allowed.