Cant assign array form initializer list

closed account (EwCjE3v7)
I'm using c++11 but this still dosn't work. In the definition of b, it works but not here. Why now? I could use std::copy to fix it but is there another way to get rid of the error?
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
26
27
28
  bool playAgain(char (&b)[11][18], string (&players)[2])
{
	char choice = 'n';

	cout << "Would you like to play again? [Y/n]" << endl;
	if (choice == 'Y' || choice == 'y') {
		b = {{' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' '}, // reset board
			 {' ',' ',' ','1',' ',' ','#',' ',' ','2',' ',' ','#',' ',' ','3',' ',' '},
			 {' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' '},
		  	 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
			 {' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' '},
			 {' ',' ',' ','4',' ',' ','#',' ',' ','5',' ',' ','#',' ',' ','6',' ',' '},
			 {' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' '},
			 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
			 {' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' '},
			 {' ',' ',' ','7',' ',' ','#',' ',' ','8',' ',' ','#',' ',' ','9',' ',' '},
			 {' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' '}};

		swap(players[0], players[1]);
		return true;
	} else if (choice == 'N' || 'n') {
		cout << "Bye, Bye." << endl;
		return false;
	} else {
		throw std::runtime_error("Only y or n. Ending!");
		return false;
	}
}

assigning to an array from an initializer list
Last edited on
You can't copy a raw array using the = operator. That's one of the reasons why C++11 added std::array.
http://www.cplusplus.com/reference/array/array/

If b is a std::array<std::array<char, 18>, 11>& then you can assign to it like you do in your code.
closed account (EwCjE3v7)
Thanks
Topic archived. No new replies allowed.