Why can't I intialize a two dimensional array in a Constructor

I get an error that says Error: expected an expression in the constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class TicTacToe
{
private:
	int board[3][3];
	
public:
	TicTacToe();
	
	~TicTacToe();

};

TicTacToe::TicTacToe()
{
	
     board[3][3] = 
	{
		{0, 0, 0},
		{0, 0, 0},
		{0, 0, 0}

	};


};
That's simply the wrong syntax.

To initialize that array to all zeroes, use this:

1
2
3
4
TicTacToe::TicTacToe()
: board()
{
}
Thanks for the quick response Cubbi, I appreciate it.

So to sum it up: arrays have a different syntax when it comes to constructors.

One more question:

I heard that it is a bad practice to add anything in a constructor other than class member initializations. Does that include for loops for the two dimensional array?
That's the same syntax as for any other member. The only catch is that C arrays couldn't be initialized to anything other than all zeroes until C++11, so yes, in that case it made sense to assign their initial values in the body of the constructor.
Topic archived. No new replies allowed.