Так что для назначения мне нужно создать программу, которая создаст магические квадраты, когда пользователь вводит нечетное число, у меня большая часть программы выполнена, но по какой-то причине, когда я пытаюсь заполнить квадраты Я получаю Необработанное исключение в 0x00326315 в magic square.exe: 0xC0000005: Место для обнаружения нарушения доступа 0x00000000Создание 2d динамического массива с указателями
Я использую классы и объявляю квадрат как 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;
}
Почему вы используете 'новый (nothrow) 'без проверки возвращаемого значения? Кроме того, дайте нам то, что компилируется. – tillaert
Учитель требует этого, я обычно не использую (nothrow) – JBoyden
Ваш учитель ДЕЙСТВИТЕЛЬНО говорит, что вы не должны проверять результат из 'нового', или просто чтобы вы использовали' nothrow'? Если это так, вы должны СЕРЬЕЗНО рассмотреть возможность поиска другого образования, так как это немного безумие. (Я как раз собирался написать что-то похожее на то, что написал tillaert, когда оно появилось) –