I have a question about template function. This is my first time using template. I may lack of some basis. I am confusing about the type of variables I should use. What is the meaning of int N after the template? I regard N as a flexible integer. Is A[N][N] 2d array variable or a pointer? What about b[N]? I have an example using all inputs as non-pointer ones. But the hint from the Xcode tells me all variable are pointer. Here is the hint "LUsolve(double (*A)[N], const double *b, double *x)". But I tried my pointers variable, not working. The function LUfactorize and LUsolve_internal are also template functions. It is too long. I don't post all code.
template < int N >
bool LUsolve( double A[N][N], const double b[N], double x[N] )
{
double B[N][N];
int i, j, p[N], status;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
B[i][j] = A[i][j];
}
}
status = LUfactorize(B, p);
if (status != 0)
{
printf("Failed in the LU factorization.\n");
return false;
}
LUsolve_internal(B, p, b, x);
return true;
}
template <int N>
int LUfactorize(double A[N][N], int p[N])
template <int N>
void LUsolve_internal(double A[N][N], const int p[N],
const double b[N], double x[N])
Here is the example I work on. The error is "No matching function for call to 'LUsolve'". How can I fix it?
bool cubicspline (double const *knots , double const *knots_value,
double *coef, int const N)
{
double x_i = 0;
double x_Km1 = 0;
double x_K = 0;
double d_i = 0;
double d_Km1 = 0;
double A [N][N];
for ( int i = 0; i < N; i++)
{
A[i][0] = 1;
A[i][1] = knots[i];
for ( int j = 2; j < N; j++)
{
x_i = ( (knots[i] > knots[j-2]) ? (knots[i] - knots[j-2]) : 0 );
x_Km1 = ( (knots[i] > knots [N-2]) ? (knots[i] - knots[N-2]) : 0 );
x_K = ( (knots[i] > knots [N-1]) ? (knots[i] - knots[N-1]) : 0 );
d_i = (x_i * x_i * x_i - x_K * x_K * x_K) / ( knots[N-1] - knots[j-2] );
d_Km1 = ( x_Km1 * x_Km1 * x_Km1 - x_K * x_K * x_K) / ( knots[N-1] - knots[N-2] );
A[i][j] = d_i - d_Km1;
}
}
bool status = LUsolve( A , knots_value, coef); // Here is the error.