c++ - 使用指针创建二维动态数组

标签 c++ arrays dynamic 2d

因此,对于一项任务,我必须创建一个程序,当用户输入一个奇数时,该程序将创建幻方,我已经完成了大部分程序,但由于某种原因,当我尝试填充正方形时,我在 0x00326315 处遇到未处理的异常在 magic square.exe: 0xC0000005: 访问冲突读取位置 0x00000000

我正在使用类并将 square 声明为 int **square;

这是代码

#include<iostream>
#include<iomanip>
#include"MagicSquare.h"

using namespace std;

MagicSquare::MagicSquare(): size(0), maxNum(0), row(0), col(0), curNum(0) //Constructor initialize variables
{
}

MagicSquare::~MagicSquare()                             //Destructor 
{
for (int i = 0; i < size; i++)
{
    delete[] square[i];
}
delete[] square;                                                                            //Delete the dynamically allocated memory
}
void MagicSquare::getSize() //Get the size of the magic square
{
    cout << "Please enter an odd number for the number of rows/columns: ";
    cin >> size;
    while (size % 2 == 0) //Check to see if the number entered is odd
    {
        cout << "The number you entered is not odd, please enter an odd number: ";
        cin >> size;
    }

    int **square = new (nothrow) int*[size];
    for (int i = 0; i < size; i++)
    {
        square[i] = new (nothrow) int[size];
    }
    maxNum = size * size;
    iCount = new (nothrow) int[size];
    jCount = new (nothrow) int[size];
}

void MagicSquare::populateSquare()
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            square[i][j] = 0;     //This is where the error occurs
        }
    }
    curNum = 1;
    col = (size - 1) / 2;
    square[row][col] = curNum;
    curNum += 1;
    for (int i = 1; i <= maxNum; i++)
    {
        row = row - 1;
        col = col + 1;
        if (col >= size)
            col = 0;
        if (row < 0)
            row = size - 1;
        square[row][col] = curNum;
        curNum += 1;
    }
}

头文件

class MagicSquare
{
private:
int **square;
int size;
int maxNum;
int row;
int col;
int curNum;
int *iCount;
int *jCount;

public:
MagicSquare();
~MagicSquare();
void getSize();
void populateSquare();
void printSquare();
};

源文件

#include"MagicSquare.h"
#include<iostream>


using namespace std;


int main() 
{
MagicSquare mySquare;

int choice = 1;
while (choice == 1)
{
    mySquare.getSize();
    mySquare.populateSquare();
    mySquare.printSquare();

    cout << "\n\nWould you like to create another magic square? 1 for yes, 0 for no: ";
    cin >> choice;
    while (choice != 1 || choice != 0)
    {
        cout << "\nInvalid input: \nWould you like to create another magic square? 1 for yes, 0 for no: ";
        cin >> choice;
    }
}

system("pause");

return 0;
}

最佳答案

您在此处的 getSize() 方法中定义了一个名为 square 的局部变量:

int **square = new (nothrow) int*[size];

因此,您为局部变量腾出了空间,但从未为类的字段腾出空间。

将这一行改为

square = new (nothrow) int*[size];

还要认真考虑检查通话结果。

关于c++ - 使用指针创建二维动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26003997/

相关文章:

c++ - 设置 Firemonkey 控件的 OnMouseMove 方法

c++ - 给定两个偶数,求出它们之间所有偶数的平方和

c++ - 设计 .h 和 .cpp 文件 C++ : Errors

php - 数组未填充第一条记录

PHP处理2列数据输入

c# 动态调用方法与表达式树

JavaScript 动态创建和填充数组

c++ - 为什么这个 C++ 字符数组似乎能够容纳超过它的大小?

javascript - 在 Json 中搜索过滤器

php数组如何在事先不知道字段名称时动态地将数据插入mysql表?