Initializing an array in constructor. [duplicate]
Possible Duplicate:
How can i use member initialization list to initialize it?
I am trying to initialize an array in my constructor and storing it in my char board[]. I am not sure how I can do this. I just want to initialize the value for board so i can replace it later.
TicTacToe::TicTacToe() : board() { }
You should read up on a little more C++ and a good place would be the article linked in the comment on your question.
I had heard that C++ did not support colon-init lists for arrays until just recently, I think.
TicTacToe() : board()
{ }
This should work, like squinlan said. If not, you can always just assign whatever values you are trying to initialize the board to within the constructor.
If you want to stick with array of char you could add the constructor:
void TicTacToe() {memset (board, 0, 9);}//this is new
to your class definition
class TicTacToe {
public:
void displayBoard();
void getMove();
void playGame();
TicTacToe() {memset (board, 0, 9);}//this is new
private:
char board[9];
char player; // Switch after each move.
};
精彩评论