C++ 记忆博弈在C+中的实现+;-内存位置超出范围错误

C++ 记忆博弈在C+中的实现+;-内存位置超出范围错误,c++,memory,exception-handling,out-of-memory,unhandled-exception,C++,Memory,Exception Handling,Out Of Memory,Unhandled Exception,因此,我一直收到一个错误,它是:ProgCW.exe中的未处理异常在_________________;处:Microsoft C++异常:std::超出内存位置的范围 我在网上找到了这段代码并实现了它,但我的实现似乎有问题 我用了一个类来做这件事 代码如下: dispGrid::dispGrid(const int gRows, const int gCols){ //declaration and allocation for creating a grid and cardstatus

因此,我一直收到一个错误,它是:ProgCW.exe中的未处理异常在_________________;处:Microsoft C++异常:std::超出内存位置的范围

我在网上找到了这段代码并实现了它,但我的实现似乎有问题

我用了一个类来做这件事

代码如下:

dispGrid::dispGrid(const int gRows, const int gCols){

//declaration and allocation for creating a grid and cardstatus
std::vector<std::vector<int>> cGrid;

int** cardStatus = new int*[gRows];
for (int i = 0; i < gRows; i++) {
    cardStatus[i] = new int[gCols];
}

//initialisation of variables
int r1, c1, r2, c2;
int moves;
bool gameOver = false;
cardShuffle(cGrid, gRows, gCols);

while (gameOver != true) { //allows to repeat over the game

    moves = 0;

    ///////////////////////////////
    //      prints grid         //
    /////////////////////////////

    //prints the coordinates 
    std::cout << "\t    ";
    for (int nCols = 0; nCols < gCols; nCols++) {
        std::cout << nCols + 1 << " ";
    }

    std::cout << std::endl;


    //initialisation
    //displays the starred grid according to difficulty level the user chose and sets cardstatus as false, for face down
    for (int nRows = 0; nRows < gRows; nRows++) {
        std::cout << "\t" << nRows + 1 << " | ";
        for (int nCols = 0; nCols < gCols; nCols++) {
            std::cout << "* ";
            cardStatus[nRows][nCols] = false;
        }
        std::cout << std::endl;
    }

    /******************************/
    /*      game mechanics       */
    /*****************************/

    do { //allows execution of thr input as long the card status is not false at that card

        //user input for the first card's coordinates
        std::cout << "Enter the first cards row: " << std::endl;
        std::cin >> r1;

        std::cout << "Enter the first cards column: " << std::endl;
        std::cin >> c1;

        if ((r1 && c1) == 0) { //checks to see whether user has input the values 0
            std::cout << "#NOTE : There are no coordinates of that value." << std::endl;
        }

    } while (cardStatus[r1 - 1][c1 - 1] != false);

    do { //allows execution of thr input as long the card status is not false at that card

         //user input for the second card's coordinates
        std::cout << "Enter the second cards row: " << std::endl;
        std::cin >> r2;

        std::cout << "Enter the second cards column: " << std::endl;
        std::cin >> c2;

        if ((r2 && c2) == 0) {//checks to see whether user has input the values 0
            std::cout << "#NOTE : There are no coordinates of that value." << std::endl;
        } 
        else if (cardStatus[r2 - 1][c2 - 1] = true) { //if the cardstatus of the second card is true then output the care is already flipped
            std::cout << "#NOTE : This card is already flipped." << std::endl;
        }
        else if ((r2 == r1) && (c2 == c1)) { //if the cards are the same then the card is already flipped
            std::cout << "#NOTE : You have already chosen this card." << std::endl;
            continue;
        }
    } while (cardStatus[r2 - 1][c2 - 1] != false);

    r1--;
    c1--;
    r2--;
    c2--;

    //reaveal cards
    //prints the coordinates 
    std::cout << "\t    ";
    for (int nCols = 0; nCols < gCols; nCols++) {
        std::cout << nCols + 1 << " ";
    }

    std::cout << std::endl;


    //initialisation
    //displays grid with blank spaces according to the coordinates then picked, setting up to replace blank spaces for values
    for (int nRows = 0; nRows < gRows; nRows++) {
        std::cout << "\t" << nRows + 1 << " | ";
        for (int nCols = 0; nCols < gCols; nCols++) {
            if ((nRows == r1) & (nCols == c1)) {
                std::cout << cGrid[nRows][nCols] << " ";
            }
            else if ((nRows == r2) && (nCols == c2)) {
                std::cout << cGrid[nRows][nCols] << " ";
            }
            else if (cardStatus[nRows][nCols] = true) {
                std::cout << cGrid[nRows][nCols] << " ";
            }
            else {
                std::cout << "* ";
            }

        }
        std::cout << std::endl;
    }

    //if match?
    if (cGrid[r1][c1] == cGrid[r2][c2]) {
        std::cout << "#NOTE : Cards Match!" << std::endl;

        cardStatus[r1][c1] = true;
        cardStatus[r2][c2] = true;
    }
    std::cin.get();
    std::cin.get();


    /*********************************************************/
    /*               prints to blank screen                  */
    /*********************************************************/

    for (int z = 0; z <= 30; z++) {
        std::cout << std::endl;
    }

    //reprint the board
    //prints the coordinates 
    std::cout << "\t    ";
    for (int nCols = 0; nCols < gCols; nCols++) {
        std::cout << nCols + 1 << " ";
    }

    std::cout << std::endl;


    //initialisation
    //displays the starred grid according to difficulty level the user chose and sets cardstatus as false, for face down
    for (int nRows = 0; nRows < gRows; nRows++) {
        std::cout << "\t" << nRows + 1 << " | ";
        for (int nCols = 0; nCols < gCols; nCols++) {
            if (cardStatus[nRows][nCols] = true) {
                std::cout << cGrid[nRows][nCols] << " ";
            }
            else {
                std::cout << "* ";
            }

        }
        std::cout << std::endl;
    }
    gameOver = true;

    //checks all card status, should all be set to true 
    for (int nRows = 0; nRows < gRows; nRows++) {
        for (int nCols = 0; nCols < gCols; nCols++) {
            if (cardStatus[nRows][nCols] == false) {
                gameOver = false;
                break;
            }
        }
        if (gameOver == false) {
            break;
        }
    }
    moves++; //counts moves
}

std::cout << "#NOTE : You have matched all the tiles" << std::endl;
std::cout << "#NOTE: You had " << moves << "moves." << std::endl << std::endl; }  

非常感谢您的帮助。

该向量为空。因此,也许我可以尝试:for(int s=0;s<20;s++)
void dispGrid::cardShuffle(std::vector<std::vector<int>> &gCards, int rows, int cols)//parameters of a 2d vector and the number of rows and columns {

int numOfElem = rows*cols;//total number of random number should be made
std::vector<int> randNum;

//generate same random numbers
for (int x = 0; x < (numOfElem / 2); x++) {
    const int fNum = rand() % 100 + 1; //generate num between 1 and 100
    int sNum;
    do {
        sNum = rand() % 100 + 1;
    } while (fNum == sNum);
    randNum.push_back(fNum);
    randNum.push_back(sNum);
}

for (int s = 0; s <= 20; s++) {
    for (int x = 0; x < numOfElem; x++) {
        srand((unsigned)time(NULL));//initialise random seed
        int i = rand() % (numOfElem - 1) + 1; //chooses a number between the length of the array
        int temp = randNum.at(x); //sets temp as the value in the at that indes in the start array
        randNum.at(x) = randNum.at(i); //sets the value at that index in the start array as the value of the index at the start array 
        randNum.at(i) = temp;
    }
}

int i = 0;

for (int nRows = 0; nRows < rows; nRows++) {// for every row and column
    for (int nCols = 0; nCols < cols; nCols++) {
        gCards.at(nRows).at(nCols) = randNum.at(i);//card at that coordinate will be equal to 
        std::cout << gCards[nRows][nCols];
        i = i + 1; 
    }
    std::cout << std::endl;
}}
gCards.at(nRows).at(nCols) = randNum.at(i); //card at that coordinate will be equal to 
for (int s = 0; s <= 20; s++)