我的程序由两个类组成。第一个创建一个二维数组并用用户输入填充它。第一个类工作正常,当我在 main 中调用它时,它能够创建和打印一个二维数组。但是,我试图将指向该二维数组的指针传递给第二个函数来计算矩阵的行列式。但是,在调用行列式函数后,我的程序不断崩溃。为什么我无法对这些数组元素进行乘法、加法或减法?
这是行列式类的实现文件:
#include <iostream>
#include "det.hpp"
using std::cout;
using std::endl;
Det::Det() {
};
int Det::determinant(int **pointerToArray, int arraySize) {
int determinant;
cout << "Calculating the determinant..." << endl;
if (arraySize == 2) {
determinant = (pointerToArray[0][0] * pointerToArray[1][1]) -
(pointerToArray[1][0] * pointerToArray[1][0]);
} else if (arraySize == 3) {
determinant = (pointerToArray[0][0] * ((pointerToArray[1][1] * pointerToArray[2][2]) -
(pointerToArray[1][2] * pointerToArray[2][1]))) -
(pointerToArray[0][1] * ((pointerToArray[1][0] * pointerToArray[1][1]) -
(pointerToArray[1][2] * pointerToArray[2][0]))) +
(pointerToArray[0][2] * ((pointerToArray[1][0] * (pointerToArray[2][1]) -
(pointerToArray[1][1] * pointerToArray[2][0])));
} else {
return determinant;
};
};
以下是我调用该函数的 main 部分:
//this is the original object
Matrix* point = new Matrix();
//this is where I retrieve the data from the first function
int * tempPoint = point->readMatrix(newArray, squareSize);
/*this is where I call the determinant with
a pointer to the original array as a parameter*/
calculate.determinant(&tempPoint, squareSize);