forward declaration 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?
1
2
//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.
1
2
3
4
5
int* p;
*p = 3;

int* a = new int[3];
a[0] = 3;


It's not a*[0] = 3;
Last edited on
Even this throws an error:
(*chessboard)[0][0];


"It's not a*[0] = 3;"

What if you had:
1
2
vector<int> a(3);
vector<int> * b = &a;


Are you telling me that I can just say:
 
b[0] = 1;

?
I doubt you can do that.

I would use and iterator because I know that would work. I have never tried to use the subscript for a vector through a pointer.
Last edited on
Topic archived. No new replies allowed.