【问题标题】:Unhandled exception at 0x00007FF982801B5F (ntdll.dll) in ConsoleApplication3.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF [duplicate]ConsoleApplication3.exe 中 0x00007FF982801B5F (ntdll.dll) 处未处理的异常:0xC0000005:访问冲突读取位置 0xFFFFFFFFFFFFFFFF [重复]
【发布时间】: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++


    【解决方案1】:
    char** board = new char*[boardSize];
    

    上面的行将您的数组设置为编译器初始化的 boardSize 的大小(很可能是 0)。它没有在构造函数中设置。 如果您可以使用调试器并查看它在板构造函数中的设置。

    如果你想要一个动态二维数组,你需要使用类似的东西

    std::vector<std::string> board;
    

    或者,将新的 char*[boardSize] 移动到构造函数中,以便您获得 boardSize 的传入值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多