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)
virtualvoid 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?
//code in class Y, doSomething()
(*chessboard)[0][0].doSomethingElse();
When you deference a pointer with the * on the lhs you are telling the compiler that you want to access the pointer's value. You can't call a method from the pointers value, only from the pointer itself. You don't have to use the * to access pointers of vectors or arrays unless they are containing pointers themselves.