0

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.

3
  • 2
    possible duplicate of How can i use member initialization list to initialize it? Commented Jul 29, 2011 at 21:02
  • 3
    Why are you not using std::vector? Commented Jul 29, 2011 at 21:03
  • 2
    Or, since the size is known at compile-time, std::array/std::tr1::array/boost::array? Commented Jul 29, 2011 at 21:06

3 Answers 3

3
  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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ironic. Did you even try to compile that? Maybe you should read up on a little more C++. ;-]
whoops, my bad on the zero, but still...
0

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.

1 Comment

Why repeat what has already been said?
0

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.
};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.