编译时出错

Error during compiling

本文关键字:出错 编译      更新时间:2023-10-16

当我编译"main.cpp: In构造函数' TicTacToe::TicTacToe() '时,我收到这个错误:Main.cpp:38:错误:数组被用作初始化器"有线索吗?"谢谢。

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

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

不能这样初始化数组。请使用strcpy

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

或者使用std::string来使用构造函数初始化式

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

"123456789"有10个字符,因为末尾有一个NULL。