Two-dimensional array of how to define the body structure assignment

Recently doing a little chess game with C ++, in order to avoid the use of global variables, I want to declare data structures to be used. To which the two-dimensional array to store information board assignment he ran question:

In the structure data is not initialized, and in the function (assuming I define a structure d), can not be used with d.map [3] [3] = {{,,,}, {,,, }}; such methods to define an array of structures in the body, such as the compiler will report an error: error 1 error C2440: "=": not from the "initializer-list" is converted to "int".

So we have to think of other ways to give this array assignment of. I ended up using a Benbanfa: use recycled done: define and initialize another array and an array of peers you need to use the same column in the function, then use the value of the loop array sequentially assigned to the newly defined structures in the body array.

It may be more convenient to assign an array of structures in the body in this way, instead of using d.map [0] [0] = ...; such a a method of assignment (I almost do so). Here is what I use to function code:

void Mouse::InitMap() {
	struct Data d;
	int mapdata[10][9] =
	{ { 'C', 'H', 'E', 'M', 'B', 'M', 'E', 'H', 'C' },
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
	{ 0, 'G', 0, 0, 0, 0, 0, 'G', 0 },
	{ 'P', 0, 'P', 0, 'P', 0, 'P', 0, 'P' },
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
	{ 'p', 0, 'p', 0, 'p', 0, 'p', 0, 'p' },
	{ 0, 'g', 0, 0, 0, 0, 0, 'g', 0 },
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
	{ 'c', 'h', 'e', 'm', 'b', 'm', 'e', 'h', 'c' } };

	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 9; j++) {
			d.map[i][j] = mapdata[i][j];
		}
	}
	PutChess(d);
}

We hope to encounter the same problem students can be helped. I wish myself quickly to complete this little project.

Released two original articles · won praise 0 · Views 52

Guess you like

Origin blog.csdn.net/weixin_45711556/article/details/105267945