gpt4 book ai didi

c++ - 程序在构造函数期间崩溃

转载 作者:行者123 更新时间:2023-11-28 02:49:31 25 4
gpt4 key购买 nike

MineSweeperBoard::MineSweeperBoard(int board_width, int board_height, int mine_count)
{
width = board_width;
height = board_height;
mined = new bool*[board_width];
for (int i = 0; i < board_width; i++){
mined[i] = new bool[board_height];
}
flagged = new bool*[board_width];
for (int i = 0; i < board_width; i++){
flagged[i] = new bool[board_height];
}
revealed = new bool*[board_width];
for (int i = 0; i < board_width; i++){
revealed[i] = new bool[board_height];
}

}

在“MineSweeperBoard”类的私有(private)部分中有**mined; **透露; **标记;为什么我的构造函数在创建这些数组之前崩溃(由于内存访问错误)?

非常感谢你的帮助,我只是不明白为什么它没有正确分配这个数组

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

这是从main调用的,一构造就崩溃

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;

最佳答案

在将它们用作数组维度之前,您必须初始化 widthheight

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

关于c++ - 程序在构造函数期间崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23377439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com