Reputation: 7281
I am trying to make a tic tac toe game in C++. I am trying to use a 2d char
array to construct the board, but when my code below goes to compile, I get an error error: cannot convert '<brace-enclosed initializer list>' to 'char' in assignment
Below is the code:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
// Player Class
class Player
{
public:
explicit Player(string name){
this->name = name;
}
string getName(){
return name;
}
string setName(string newname){
name = newname;
}
int generateRowMovement(){
int row = rand() % 2; // Random number between 0 and 2
return row;
}
int generateColMovement(){
int col = rand() % 2; // Random number between 0 and 2
return col;
}
int placeMove(){
int rowMove, colMove;
rowMove = rand() % 2;
colMove = rand() % 2;
return rowMove, colMove;
}
private:
string name;
};
//Board Class
class Board
{
public:
explicit Board(){
this->state[3][3] = {
{'.', '.', '.'},
{'.', '.', '.'},
{'.', '.', '.'}}
} // end board
void printBoard(char state[3][3]){
for (int i =0; i < 3; i ++){
for (int j =0; j < 3; j++){
cout << state[i][j] << " ";
}
cout << endl;
}
} //end printboard
void printBoard(){
for (int i =0; i < 3; i ++){
for (int j =0; j < 3; j++){
cout << this->state[i][j] << " ";
} //end inner j for
cout << endl;
} // end outer i for
} //end printboard
private:
char state[3][3];
};
int main(){
// Player Tony("Tony");
// cout << Tony.getName();
Player Tony("Tony");
Board game();
cout << Tony.getName() << endl;
Tony.setName("Bob");
cout << Tony.getName() << endl;
int currRow, currCol;
currRow = Tony.generateRowMovement();
currCol = Tony.generateColMovement();
cout << currRow << currCol << endl;
return 0;
} //end main
If I add the following block to main
(and adjust some other things):
char grid[3][3] = { {'.', '.', '.'},
{'.', '.', '.'},
{'.', '.', '.'}};
for (int i =0; i < 3; i ++){
for (int j =0; j < 3; j++){
cout << grid[i][j] << " ";
} //end inner j for
cout << endl;
} // end outer i for
I get the expected result of:
. . .
. . .
. . .
Why can I not make the default construction of this object a [3][3] char
array like above?
Upvotes: 2
Views: 130
Reputation: 1002
Why can I not make the default construction of this object a [3][3] char array like above?
Yes, you can.
explicit Board() : state({
{'.', '.', '.'},
{'.', '.', '.'},
{'.', '.', '.'}}) {
} // end board
this->state[3][3] =
is variable assignment statement, you cannot use brace enclosed list. But you can use it in constructor.
Upvotes: 0
Reputation: 598174
this->state[3][3] = {...}
is not a valid assignment. You can't assign arrays that way. And brace initialization only works in a declaration, not in an assignment. Your Board
constructor needs to use loops to fill the state
member:
explicit Board(){
for (int i =0; i < 3; i ++){
for (int j =0; j < 3; j++){
state[i][j] = '.';
}
}
}
Upvotes: 1
Reputation: 4186
Array brace initialization works only when declaring an array.
If you are using C++1x try adding the initialization statement {...}
to the member declaration:
private:
char state[3][3] = {...};
Alternatively you can fill the array using std::fill
or std::fill_n
:
std::fill (&state[0][0], &state[3][3], '.');
std::fill_n(&state[0][0], sizeof(state)/sizeof(state[0][0]), '.');
Upvotes: 2