I want to implement a matrix class, which is a template with data members as a two-dimensional array. When I recursively call in a template function, the compiler reports an error saying that the array is missing an index. What should I do? The following code is a simplified version
#include <iostream>
template<typename T, int N>
class Matrix
{
public:
Matrix()
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
Data[i][j] = 0;
}
}
}
private:
T Data[N][N];
};
template<typename T, int N>
Matrix<T, N> compute(const Matrix<T, N>& vA, const Matrix<T, N>& vB)
{
Matrix<T, N> C;
if (N != 1)
{
const int HalfN = N >> 1;
Matrix<T, HalfN> HalfA, HalfB, HalfC;
HalfC = compute<T, HalfN>(HalfA, HalfB);
}
return C;
}
int main()
{
const int n = 32;
Matrix<int, n> A;
Matrix<int, n> X;
X = compute<int, n>(A, A);
return 0;
}
I still want to use two-dimensional arrays in template classes, how should I modify them