Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 构造函数期间程序崩溃_C++_Arrays_Pointers_Dynamic_Constructor - Fatal编程技术网

C++ 构造函数期间程序崩溃

C++ 构造函数期间程序崩溃,c++,arrays,pointers,dynamic,constructor,C++,Arrays,Pointers,Dynamic,Constructor,这就是所谓的从主,崩溃,一旦它被建造 class MineSweeperBoard { public: // Initialize a board with a given width and height, containing the // given number of randomly-placed mines. Nothing should be revealed // or flagged. If there are more mines than spa

这就是所谓的从主,崩溃,一旦它被建造

class MineSweeperBoard
{
public:
    // Initialize a board with a given width and height, containing the
    // given number of randomly-placed mines.  Nothing should be revealed
    // or flagged.  If there are more mines than spaces for them, fill
    // the entire board with mines.
    MineSweeperBoard(int board_width, int board_height, int mine_count);
    // Clean up the board, freeing any dynamically allocated memory.
    //~MineSweeperBoard();

    // Get the size of the board.
    int get_width() const;
    int get_height() const;

    // Reveal a square.
    // If this square wasn't already revealed, and if the number of
    // adjacent mines equals the number of adjacent flags, recursively
    // reveal all the surrounding squares that aren't flagged.
    void reveal(Position p);

    // Put a flag on a square, or remove a flag that is
    // already there.  Returns true if we placed a flag, false if
    // we removed one.
    bool flag(Position p);

    // Return the appearance of the square (what will be
    // displayed to the player) as a single character.
    char appearance(Position p) const;

    // Display the entire board to an output stream.  Prints
    // a header with the column numbers, and prints the row
    // number on each line.  For example,
    //     |  0  1  2  3  4
    //  ---+---------------
    //   0 |  1  /  .  .  .
    //   1 |  1  2  .  .  .
    //   2 |  0  1  .  .  .
    //   3 |  0  1  2  1  1
    //   4 |  0  0  0  0  0
    void display() const;

    // Returns true if the player won (every square with a mine
    // is flagged, and every cell without a mine is revealed).
    bool won() const;

    // Returns true if the player lost (there is a revealed
    // mine).
    bool lost() const;

    // Reveal everything (losing the game in the process)
    void give_up();
private:
    // Returns a list of all the positions adjacent to p.  If p
    // is in the middle of the board, it has eight neighbors,
    // but if it is on an edge or corner it will have fewer.
    PositionList adjacent(Position p) const;

    // Return the number of mines or flags adjacent to a square.
    int adjacent_mines(Position p) const;
    int adjacent_flags(Position p) const;

    // Size of the board.
    int width;
    int height;

    // Dynamically allocated 2D arrays indicating which squares are
    // revealed, which are mined, and which are flagged.
    bool **revealed;
    bool **mined;
    bool **flagged;

};
#endif
intmain()
{
srand(static_cast(time(NULL));
整数宽度;
内部高度;
国际矿山;
cout>高度;
cin.clear();
库特宽度;
cin.clear();
库特地雷;
cin.clear();
扫雷板(宽度、高度、地雷);
位置p;

在将宽度和高度用作数组维度之前,必须初始化宽度和高度

int main()
{
    srand(static_cast<unsigned int>(time(NULL)));
    int width;
    int height;
    int mines;
    cout << "Please enter a board height: 5-20 ";
    cin >> height;
    cin.clear();
    cout << endl << "Please enter the board width: (5-20) ";
    cin >> width;
    cin.clear();
    cout << endl << "Please enter the number of mines: ";
    cin >> mines;
    cin.clear();
    MineSweeperBoard board(width, height, mines);
    Position p;

必须先初始化变量
挖掘的
显示的
,以及
标记的
,然后才能运行此操作

它们必须指向准备接受分配的指针:

MineSweeperBoard::MineSweeperBoard(int board_width, int board_height, int mine_count)
{
    width = board_width;
    height = board_height;
    **mined = new int * [mine_count];
    **revealed = new int * [width*height];
    **flagged =new int * [width*height];
}

或者,根据这些变量的类型,将
***
替换为
*

你确定需要为地雷、旗帜和启示使用单独的数组吗?AFAICS,一个字节
无符号字符
有足够的空间。低3位用于相邻地雷,1位用于挖掘,1位用于揭示,哇,还有3位。我更新d代码,但我的构造函数仍在崩溃,我在扫雷板的私有部分定义了bool mined;bool**discovered;和boolflagged请显示类定义。显示如何构造此类的对象OK,我添加了来自main的调用。我更改了我的构造函数,但它仍在崩溃。
MineSweeperBoard::MineSweeperBoard(int board_width, int board_height, int mine_count)
{
    width = board_width;
    height = board_height;
    **mined = new int * [mine_count];
    **revealed = new int * [width*height];
    **flagged =new int * [width*height];
}
int *p1;
mined = &p1;
**mined = new int * [mine_count];