class containing 2d vector

I have a class containing a 2d vector of type Square called chessboard:


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



Square objects have a pointer that points to a GamePiece object which is parent to all the different chess pieces.

Here's the problem I'm having. GamePiece has to have a pointer to chessboard so that it can calculate its legal moves on the board:

1
2
3
4
5
class GamePiece
{
...
vector< vector<Square> > * chessboard; //needs to point to same chessboard as in class A
}


I (think I) fixed the circular dependency between Square and GamePiece with forward declarations, but now I cannot for the life of me initialize chessboard to an 8x8 array of Squares.

Here are my questions:
1. Does the 2d vector need to be initialized on the heap in order for GamePiece to be able to access it with a pointer? If so, how can I initialize it on the heap? I've tried this:

1
2
3
4
5
6
7
8
9
10
11
class A
{
...
vector< vector<Square> > * chessboard;
}

A::A()
{
chessboard = new vector(8, vector<Square>(8));
}


...And it doesn't compile. I can't figure it out.

Any suggestions on a better solution? All I need is an 8x8 array that both class A and GamePiece can see (and I have to manage memory manually, which is why I chose vectors to simplify things).


Vectors have a resize() method so just use that.

1
2
3
4
5
	chessboard.resize(8);

	for (int i = 0; i < 8; i++)
	    chessboard[i].resize(8);


It fills them with zeros so if you are going to load the vectors with actual values you could use a push_back instead of manually setting the size.


To access it with a pointer you could declare an iterator to a vector of that type. An iterator is a pointer.


 
vector< vector<Square> >::iterator it;

Last edited on
So if I resize my vector to 8x8 in A's constructor, and I send &chessboard over to a GamePiece object, it would be able to see the chessboard using that pointer as long as A isn't destroyed, right?
I don't see why it fail to compile. What is the error message you get? No you don't need to allocate on the heap. But don't confuse the heap with storage duration. In your first code chessboard may very well be on the heap if the A object was created with new. You can always get the pointer to an object by using the & operator.

I don't see the benefit of calling resize instead of using the constructor.
Last edited on
If I try to compile

1
2
3
4
5
6
7
8
9
10
class ChessModel
{
...
vector< vector<Square> > * chessboard;
}

ChessModel::ChessModel()
{
chessboard = new vector(8, vector<Square>(8));
}



I get this:

model/src/ChessModel.cpp: In constructor ‘ChessModel::ChessModel()’:
model/src/ChessModel.cpp:13:19: error: expected type-specifier before ‘vector’
model/src/ChessModel.cpp:13:19: error: cannot convert ‘int*’ to ‘std::vector<std::vector<Square> >*’ in assignment
model/src/ChessModel.cpp:13:19: error: expected ‘;’ before ‘vector’


If I don't allocate it on the heap, is there a way to initialize it to 8x8 without using resize?
Last edited on
You need to put the constructor declaration inside the class definition, and don't forget the semicolon after the class definition.

1
2
3
4
5
6
7
class ChessModel
{
public:
	ChessModel();
private:
	vector< vector<Square> > * chessboard;
};


If you can avoid pointers and dynamic allocations it's always nice.
1
2
3
4
5
6
7
8
9
10
11
class ChessModel
{
public:
	ChessModel();
private:
	vector< vector<Square> > chessboard;
};
ChessModel::ChessModel()
:	chessboard(8, vector<Square>(8));
{
}
"So if I resize my vector to 8x8 in A's constructor, and I send &chessboard over to a GamePiece object, it would be able to see the chessboard using that pointer as long as A isn't destroyed, right?"

Sure. But if you are doing it like that, can't you just load the vector to whatever size your data is? I mean what if you want to have a 16x16 board?

If you use push_back when loading the board you can fill it to whatever size you want depending on how much data you have or if you store the size in a variable.

Hope this helps.
8x8 is quite standard for a chessboard

If he really wants another size it's not hard to just pass the size to the constructor and change the numbers passed to the constructor.
Last edited on
For some reason my super constructor call isn't working.

This doesn't work:
1
2
3
Rook::Rook(vector< vector<Square> > * board) : GamePiece(board)
{
}


But this does:
1
2
3
4
Rook::Rook(vector< vector<Square> > * board) : GamePiece(board)
{
      chessboard = board;
}


Why isn't the super constructor doing anything? This is the code for it:
1
2
3
GamePiece(vector< vector<Square> > * board) : chessboard(board)
{
}
Are you sure that you didn't put a chessboard variable in the class declaration for Rook or something?
yeah I did, and I accidentally made it private not protected in GamePiece. Thanks though, I fixed it!
Topic archived. No new replies allowed.