在我的程序中,我试图使用间接递归来计算矩阵(2D数组)的行列式来执行辅助因子扩展(即两个函数递归地相互调用)。但是,编译器抱怨它无法将double **转换为const double **,即使在递归中将const参数传递给另一个const参数应该是合法的,对吧?我做错了什么?
Compliation
make clean && make
rm -f *.o determinant
clang++-3.5 -g -Wall -std=c++11 -c determinant.cpp -o determinant.o
determinant.cpp:26:13: error: no matching function for call to 'determinant'
cout << determinant(matrix, MATRIX_SIZE) << endl;
^~~~~~~~~~~
./determinant.h:7:8: note: candidate function not viable: no known conversion
from 'double **' to 'const double **' for 1st argument
double determinant(const double** matrix, int n);
^
determinant.cpp:71:15: warning: implicit conversion of NULL constant to 'int'
[-Wnull-conversion]
int colRef = NULL;
~~~~~~ ^~~~
0
determinant.cpp:85:48: error: no matching function for call to 'determinant'
double cofactor = pow(-1, i) * matrix[i][0] * determinant(submatrix, n-1);
^~~~~~~~~~~
determinant.cpp:42:8: note: candidate function not viable: no known conversion
from 'double **' to 'const double **' for 1st argument
double determinant(const double** matrix, int n)
^
1 warning and 2 errors generated.
make: *** [determinant.o] Error 1
源文件
#include <iostream>
#include <iomanip>
#include <math.h>
#include "determinant.h"
using namespace std;
int main()
{
// Matrix initialization
const int MATRIX_SIZE = 5;
double** matrix = new double*[MATRIX_SIZE];
for (int i = 0; i < MATRIX_SIZE; i++)
{
matrix[i] = new double[MATRIX_SIZE];
}
matrix[0][0] = 4; matrix[0][1] = 0; matrix[0][2] = -7; matrix[0][3] = 3; matrix[0][4] = -5;
matrix[1][0] = 0; matrix[1][1] = 0; matrix[1][2] = 2; matrix[1][3] = 0; matrix[1][4] = 0;
matrix[2][0] = 7; matrix[2][1] = 3; matrix[2][2] = -6; matrix[2][3] = 4; matrix[2][4] = -8;
matrix[3][0] = 5; matrix[3][1] = 0; matrix[3][2] = 5; matrix[3][3] = 2; matrix[3][4] = -3;
matrix[4][0] = 0; matrix[4][1] = 0; matrix[4][2] = 9; matrix[4][3] = -1; matrix[4][4] = 2;
// Determinant should be 6.
cout << determinant(matrix, MATRIX_SIZE) << endl;
// Recycles memory used by matrix.
for (int i = 0; i < MATRIX_SIZE; i++)
{
delete[] matrix[i];
}
delete[] matrix;
return 0;
}
// Predefine: matrix is a two dimensional array representing a square n x n matrix.
// n is the dimension of the matrix.
// Returns the determinant of the given matrix.
double determinant(const double** matrix, int n)
{
switch (n)
{
// If the matrix is a 1x1 matrix, the determinant is equal to itself.
case 1:
return matrix[0][0];
default:
return cofactorExpansion(matrix, n);
}
}
// Predefine: matrix is a two dimensional array representing a square n x n matrix.
// n is the dimension of the matrix.
// i is the current iteration of monomial in the cofactor expansion (by default zero).
// Returns the determinant of a matrix where n > 2.
double cofactorExpansion(const double** matrix, int n, int i)
{
// By definition of cofactor expansion, the determinant of A is equal to
// (-1)^2 * a[1][1] * det(A[1][1]) + ... +(-1)^(i+1) * a[1][i] * det(A[1][j]) + ... + (-1)^(n+1) * a[1][n] * det(A[1][n]) (not assuming zero based indexing);
// where a[i][j] is the value at row i and column j, and A[i][j] is the
// matrix not including the ith row, and jth column. A cofactor is
// defined to be one monomial of the cofactor expansion.
if (i < n)
{
// Creates an (n-1)x(n-1) square matrix removing the first row and ith column.
double** submatrix = new double*[n-1];
int colRef = NULL;
for (int subCol = 0; subCol < n-1; subCol++)
{
colRef = ((subCol < i) ? subCol : subCol + 1);
submatrix[subCol] = new double[n-1];
for (int subRow = 0; subRow < n-1; subRow++)
{
submatrix[subCol][subRow] = matrix[colRef][subRow+1];
}
}
// Performs calculation of a cofactor for the column i.
double cofactor = pow(-1, i) * matrix[i][0] * determinant(submatrix, n-1);
// Recycles the memory of the submatrix.
for (int subCol = 0; subCol < n-1; subCol++)
{
delete[] submatrix[subCol];
}
delete[] submatrix;
return cofactor + cofactorExpansion(matrix, n, i+1);
}
else
{
return 0;
}
}
头文件只包含两个函数的声明,并没有什么特别之处。