【发布时间】:2020-12-31 08:26:07
【问题描述】:
我在尝试初始化类中的动态二维数组时收到此错误“ConsoleApplication3.exe 中 0x00007FF982801B5F (ntdll.dll) 处的未处理异常:0xC0000005:访问冲突读取位置 0xFFFFFFFFFFFFFFFF”。
这是我的课
class Board {
private:
int boardSize;
char** board = new char*[boardSize];
public:
Board() {
for (int i = 0; i < this->boardSize; i++) {
this->board[i] = new char[this->boardSize];
}
for (int i = 0; i < this->boardSize; i++) {
for (int j = 0; j < this->boardSize; j++) {
this->board[i][j] = '-';
}
}
}
/*Board(int boardSize) {
this->boardSize = boardSize;
for (int i = 0; i < this->boardSize; i++) {
this->board[i] = new char[this->boardSize];
}
for (int i = 0; i < this->boardSize; i++) {
for (int j = 0; j < this->boardSize; j++) {
this->board[i][j] = '-';
}
}
} */
Board(int boardSize, int c1r, int c1c, int c2r, int c2c) {
this->boardSize = boardSize;
for (int i = 0; i < this->boardSize; i++) {
board[i] = new char[this->boardSize];
}
for (int i = 0; i < this->boardSize; i++) {
for (int j = 0; j < this->boardSize; j++) {
board[i][j] = '-';
}
}
this->board[c1r][c1c] = 'C';
this->board[c2r][c2c] = 'C';
this->board[boardSize / 2][boardSize] = 'B';
}
~Board() {
for (int i = 0; i < this->boardSize; i++) {
delete[] this->board[i];
}
delete[] this->board;
}
void print_board() {
for (int i = 0; i < this->boardSize; i++) {
for (int j = 0; j < this->boardSize; j++) {
std::cout << board[i][j] << "|";
}
std::cout << "\n";
}
}
};
这是我的主要功能
int main() {
Animals duck;
duck.move('d');
std::cout << duck.getColumn() << "\n";
Board playing_board(12, 2,2,4,4);
playing_board.print_board();
}
谢谢
【问题讨论】:
标签: c++