Issue initializing 2d array

So i have absolutely no idea whats happening here but I'm sure I'm just making some kind of sad beginner's error and I can't see it. If someone could just take a look at this and tell me whats going on here...

Heres the definition in the header file...
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

class symgame
{
private:
	char matrixmins[9][9];
(...)


And heres where the problem is, in the class initializer...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include "symmetrix_master.h"
using namespace std;

symgame::symgame()
{
	
	matrixmins[9][9] = { 
		{ ' ',' ',' ',' ','4',' ',' ',' ',' ' },
		{ ' ',' ',' ','4','3','4',' ',' ',' ' }, 
		{ ' ',' ','4','3','2','3','4',' ',' ' },
		{ ' ','4','3','2','1','2','3','4',' ' },
		{ '4','3','2','1','0','1','2','3','4' },
		{ ' ','4','3','2','1','2','3','4',' ' },
		{ ' ',' ','4','3','2','3','4',' ',' ' },
		{ ' ',' ',' ','4','3','4',' ',' ',' ' },
		{ ' ',' ',' ',' ','4',' ',' ',' ',' ' }
		};

	
}


And as for errors, I'm getting 27 of them alternating between "missing ';' before '}'", missing ';' before ','" and "missing ';' before '{'" ...

As far as I can tell I'm doing exactly what you're supposed to when one wants to initialize specific values for a 2dimensional like this, so am I just making some crap typo?

Any help would be greatly appreciated, thanks folks :)

[EDIT] By the way if it isn't clear, the array's non-number values are spaces. And the numbers are listed as characters, note the '''s. This is on purpose.
Last edited on
Did you put ';' after class definition ( at the end of class definition )
By the way , you don't need to write
1
2
3
#include <iostream>

using namespace std ;

again in cpp file if you already wrote that in header file.
Last edited on
Yes I did, heres the full class definition, if it helps
1
2
3
4
5
6
7
8
9
10
11
12
class symgame
{
private:
     char matrixmins[9][9];
     int betnum, betmoves, betcash, ansmoves[9][9], anscash;

public:
     symgame();
     void placebet();
     void rungame();
     void showresult();
};


I havn't written any of these things yet, waiting to debug and finish the damn initializer haha.

And thanks, good to know :D
Still doesn't work however, with or without that semicolon. Tried a few other things too, even tried using a vector instead of a regular array but I didn't understand enough about it and it just created more errors without getting rid of the old ones.

So, simply put, I still need help haha
Try this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
symgame::symgame()
{
        char temp[9][9] = { 
		{ ' ',' ',' ',' ','4',' ',' ',' ',' ' },
		{ ' ',' ',' ','4','3','4',' ',' ',' ' }, 
		{ ' ',' ','4','3','2','3','4',' ',' ' },
		{ ' ','4','3','2','1','2','3','4',' ' },
		{ '4','3','2','1','0','1','2','3','4' },
		{ ' ','4','3','2','1','2','3','4',' ' },
		{ ' ',' ','4','3','2','3','4',' ',' ' },
		{ ' ',' ',' ','4','3','4',' ',' ',' ' },
		{ ' ',' ',' ',' ','4',' ',' ',' ',' ' }
		};
		for( int i = 0 ; i < 9 ; i++ )
		     for( int j = 0 ; j < 9 ; j++ )
		          matrixmins[i][j] = temp[i][j] ;
}

Another way and long way is assiging characters 1 by 1 . Like
1
2
3
4
5
matrixmins[0][0] = ' ';
matrixmins[0][1] = 'a';
matrixmins[0][2] = 'b';
...
...
Last edited on
Sweet it worked :D I have no idea why it worked...but it worked. Can you explain what that changed?

Also I now have an unresolved external, but I'm 99% positive that its just upset I havn't written a main(); yet.

[Edit]And I started assigning them one by one and then decided I'd either wait for a better solution or blow up my computer. Fortunately your temp[9][9] solution worked.
Last edited on
matrixmins[9][9] means that ,element of matrixmins which is at pozition 9.th column 9.th row.
it isn't initialization.
Last edited on
Ahh yes, I figured that at one point and tried

1
2
3
4
matrixmins = {
{ .... }
   ''
};


But that didn't work either, so I guess using the temp is the only way to go.
This doesn't work in the constructor because it is simply illegal syntax. Unfortunately you can only initialize arrays like this. Within the body of the constructor you can only set the values because the construction of the array has already occurred by then. This bracket notation is only good for initializing arrays, unfortunately.
1
2
3
4
5
6
7
8
9
10
11
 matrixmins[9][9] = { 
		{ ' ',' ',' ',' ','4',' ',' ',' ',' ' },
		{ ' ',' ',' ','4','3','4',' ',' ',' ' }, 
		{ ' ',' ','4','3','2','3','4',' ',' ' },
		{ ' ','4','3','2','1','2','3','4',' ' },
		{ '4','3','2','1','0','1','2','3','4' },
		{ ' ','4','3','2','1','2','3','4',' ' },
		{ ' ',' ','4','3','2','3','4',' ',' ' },
		{ ' ',' ',' ','4','3','4',' ',' ',' ' },
		{ ' ',' ',' ',' ','4',' ',' ',' ',' ' }
		}; 




Notice that in this example, the temp array is being constructed/initialized at the same time. It is a temporary array created with the stack memory. Then the for loops simply copy the data from the temp into the class attribute.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
symgame::symgame()
{
        char temp[9][9] = { 
		{ ' ',' ',' ',' ','4',' ',' ',' ',' ' },
		{ ' ',' ',' ','4','3','4',' ',' ',' ' }, 
		{ ' ',' ','4','3','2','3','4',' ',' ' },
		{ ' ','4','3','2','1','2','3','4',' ' },
		{ '4','3','2','1','0','1','2','3','4' },
		{ ' ','4','3','2','1','2','3','4',' ' },
		{ ' ',' ','4','3','2','3','4',' ',' ' },
		{ ' ',' ',' ','4','3','4',' ',' ',' ' },
		{ ' ',' ',' ',' ','4',' ',' ',' ',' ' }
		};
		for( int i = 0 ; i < 9 ; i++ )
		     for( int j = 0 ; j < 9 ; j++ )
		          matrixmins[i][j] = temp[i][j] ;
} 



I've always found that to be an annoying aspect of C++ especially when dealing with arrays as class attributes and I can't think of a better solution honestly. Since the initialization of your array seems to be following a pattern you could try to write an algorithm that performs the initialization.
Last edited on
I probably will, as I'll be rearranging the values alot. I would just basically do a function that takes in the values and uses them in a temp like above, right?
Topic archived. No new replies allowed.