Я пытаюсь передать 2D-массив в функцию. К сожалению, я застрял с довольно старым компилятором (g cc -4.1) и не могу использовать какие-либо современные методы. После небольшого поиска в Google и обсуждения потоков. Я придумал это. MatrixMN is own implementation of matrices
namespace MatrixMNTest {
// Test variables
double matrix_4X2[4][2] = {{1, 2}, {3, 4}, {11, 12}, {0, 1}};
template <size_t size1, size_t size2>
MatrixMN getMatrix(const double (&arr)[size1][size2]) {
MatrixMN m(size1, size2);
for (unsigned i = 0; i < size1; ++i) {
for (unsigned j = 0; j < size2; ++j) {
m(i, j) = arr[i][j];
}
}
return m;
}
}
int main() {
size_t size1 = 4;
size_t size2 = 2;
// success
math::MatrixMN A = MatrixMNTest::getMatrix(MatrixMNTest::matrix_4X2);
double scalar = 2.5;
double result[size1][size2];
memcpy(result, MatrixMNTest::matrix_4X2, sizeof(result));
// fail
math::MatrixMN B = MatrixMNTest::getMatrix(result);
}
Я попытался запустить один и тот же код на g cc -4.1 и 7.5.0, чтобы проверить сообщение об ошибке
g cc -4.1
error: no matching function for call to 'getMatrix(double [(((unsigned int)(((int)size1) - 1)) + 1u)][(((unsigned int)(((int)size2) - 1)) + 1u)])'
g cc - 7.5.0
test.cpp: In function ‘int main()’:
test.cpp:96:52: error: no matching function for call to ‘getMatrix(double [size1][size2])’
math::MatrixMN B = MatrixMNTest::getMatrix(result);
^
test.cpp:69:16: note: candidate: template<long unsigned int size1, long unsigned int size2> math::MatrixMN MatrixMNTest::getMatrix(const double (&)[size1][size2])
math::MatrixMN getMatrix(const double (&arr)[size1][size2]) {
^~~~~~~~~
test.cpp:69:16: note: template argument deduction/substitution failed:
test.cpp:96:52: note: variable-sized array type ‘long int’ is not a valid template argument
math::MatrixMN B = MatrixMNTest::getMatrix(result)
Не уверен..Как решить эту проблему. Любая помощь с благодарностью.