C++的调试错误真的很令人沮丧

Really frustrating debug Error with C++

本文关键字:真的 调试 错误 C++      更新时间:2023-10-16

所以我正试图在C++中制作一个非常基本的TicTacToe,虽然我没有明显的语法错误,但我有很多调试错误:"Cobra.exe中0x0100142D处的未处理异常:0xC0000005:读取位置0xCCCCCC359的访问冲突"

我觉得这是一个明显的错误,我只是没有处理,但它;It’我肯定开始紧张了。我会标记访问错误的位置……现在它;s在我的将赢方法中,但我觉得肯定不止一个。。

在我的标题中,我使用了一个私有的char**板和一个私有int播放器。

#include "TicTacToe.h"
#include <iostream>
using namespace std;
int main()
{
    int rowChosen,
        colChosen;
    TicTacToe newGame;
    while(newGame.checkWin()==' ' && !newGame.fullBoard())
    {
        newGame.displayBoard();
        do
        {
            cout << "Player " << newGame.getPlayer() << " choose a row and column.";
            cin >> rowChosen >> colChosen;
            newGame.setGame(rowChosen,colChosen);
        }while(newGame.setGame(rowChosen,colChosen)==false);
        newGame.makeMove(rowChosen, colChosen, newGame.getPlayer());
        newGame.switchPlayer();
    }
    newGame.displayBoard();
    if(newGame.checkWin()!=' ')
        cout << "Player " << newGame.returnWinner() << " wins!";
    else if(newGame.fullBoard()==true)
        cout << "Cat's Game: This is a Draw!";
    return 0;
}
TicTacToe::TicTacToe()
{
    player = 1;
    char blank = ' ';                       
    for(int row=0;row<3;row++)              
        for(int col=0;col<3;col++)
            board[row][col] = ' ';
}
void TicTacToe::setPlayer(int play)
{
    player = play;
}

int TicTacToe::getPlayer()
{
    return player;
}
void TicTacToe::switchPlayer()
{
    if (player==1)
        player++;
    else
        player--;
}
bool TicTacToe::setGame(int row, int col)       //input validation
{
    if (row >= 3 || row < 0) 
        return false;
    if (col >= 3 || col < 0) 
        return false; 
    if (board[row][col] != ' ')
        return false;
    return true;
}
char TicTacToe::getBoard(int row, int col)
{
    return board[row][col];
}
bool TicTacToe::fullBoard()
{
    bool full = true;
    for(int row=0;row<3;row++)  
        for(int col=0;col<3;col++)
        {
            if(board[row][col]==' ')
            {
                full=false;
                break;
            }       
        }
    return full;    
}
void TicTacToe::makeMove(int r, int c, int player)
{
    char ch;
    if (player==1)
        ch = 'X';
    else
        ch = 'O';
    board[r][c] = ch;
}
char TicTacToe::checkWin()
{
    char b = ' ';
    for(int i=0; i<3; i++) //horizontal
    {
        if((board[i][1]==board[i][0]) && (board[i][1]==board[i][2])) //THIS IS ERROR
        {
            b=board[i][1];
        }
    }
    for(int j=0; j<3; j++) //vertical
    {
        if( (board[1][j]==board[0][j]) && (board[1][j]==board[2][j]) ) 
            b=board[1][j];
    }
    if((board[0][0]==board[1][1] && board[1][1]==board[2][2]) ||
       (board[2][0]==board[1][1] && board[1][1]==board[0][2]))
        b= board[1][1];
    return b;   
}
void TicTacToe::displayBoard()
{
    for(int row=0;row<3;row++)
    {
        cout << "|-----|";
        for(int col=0;col<3;col++)
        {
            if(board[row][col]==' ')
                cout << "| ";
            else
                cout << "|" << board [row][col];
        }
        cout << "|" << endl;
        cout << "|-----|";
    }
}
int TicTacToe::returnWinner()
{
    int winner = 0;
    if(checkWin()=='X')
        winner = 1;
    else if(checkWin()=='O')
        winner = 2;
    return winner;
}

这是我的标题TicTacToe.h

class TicTacToe
{
 private:
    char board[3][3];     //there we go
    int player;
 public:
    TicTacToe();
    void setPlayer(int);
    int getPlayer();
    void switchPlayer();
    bool setGame(int,int);
char getBoard(int,int);
    bool fullBoard();
    void makeMove(int,int,int);
    char checkWin();
    void displayBoard();
    int returnWinner();
};

假设您已经包含了所有的代码,那么您就缺少了一些重要的东西:类TicTacToe的声明。当然,有一个TicTacToe.h,所以它可能(可能)藏在里面。那会有帮助的。

话虽如此,我认为Mat说得对——你在ctor中声明一个名为board的本地并初始化它。但一旦ctor完成,这就超出了范围,这意味着所有的初始化都会消失在以太中。。假设您在类中定义了board,那么只需删除char声明就可以了。

现在,这里有一个一般的建议:如果你遇到访问冲突异常,通常意味着你对所指内容的范围犯了错误。由于异常发生在你使用board的地方,因此可以查看包含已识别board的程序的切片