user868756
user868756

Reputation: 21

"Array used as initializer" error during compiling

I am receiving this error when i compile "main.cpp: In constructor ‘TicTacToe::TicTacToe()’: main.cpp:38: error: array used as initializer " Any clues? thanks.

class chicken {
public:
    chicken() : board("123456789") {};



private:
    char board[10];
    char player; // Switch after each move.
};

Upvotes: 0

Views: 186

Answers (2)

Mahesh
Mahesh

Reputation: 34625

You cannot initialize the array like that. Instead use strcpy.

TicTacToe() {
      strcpy(board,"123456789") 
}

Or use std::string to make use of constructor initializer.

class TicTacToe {
public:
    TicTacToe() : board("123456789") // Ok
    {};

private:
    std::string board ; // Changed to string type. There is no good reason to use 
                        // character array when string type is available.
};

Upvotes: 3

QuentinUK
QuentinUK

Reputation: 3077

"123456789" has 10 characters because there is a NULL at the end.

Upvotes: 0

Related Questions