error: tipos incompatibles en la asignación de 'long int (*) [4]' a 'long int [4] [4]'
Estoy tratando de construir el mío.Matrix
tipo que actúa en línea un estándarC
matriz con matrices multidimensionales. Hasta ahora, esta es mi implementación:
#include <iostream>
/**
* To build it use:
* g++ -std=c++11 test_template_initicialization.cpp -o main
*/
template <int width, int height>
struct Matrix
{
long int _data[height][width];
Matrix()
{
}
Matrix(long int matrix[height][width]) : _data(matrix)
{
}
/**
* Overloads the `[]` array access operator, allowing you to access this class objects as the
* where usual `C` arrays.
*
* @param line the current line you want to access
* @return a pointer to the current line
*/
long int* operator[](int line)
{
return this->_data[line];
}
/**
* Prints a more beauty version of the matrix when called on `std::cout<< matrix << std::end;`
*/
friend std::ostream &operator<<( std::ostream &output, const Matrix &matrix )
{
int i, j;
for( i=0; i < height; i++ )
{
for( j=0; j < width; j++ )
{
output << matrix._data[i][j] << ", ";
}
output << matrix._data[i][j] << "\n";
}
return output;
}
};
/**
* C++ Matrix Class
* https://stackoverflow.com/questions/2076624/c-matrix-class
*/
int main (int argc, char *argv[])
{
Matrix<3, 3> matrix;
std::cout << matrix << std::endl;
matrix[0][0] = 911;
std::cout << matrix << std::endl;
std::cout << matrix[0] << std::endl;
std::cout << matrix[0][0] << std::endl;
Matrix<4,4> matrix2 = { 0 };
}
Cuando construyas el último ejemploMatrix<4,4> matrix2 = { 0 };
Recibo un error de tipos incompatibles:
D:\test_template_initicialization.cpp: In instantiation of 'Matrix<width, height>::Matrix(long int (*)[width]) [with int width = 4; int height = 4]':
D:\test_template_initicialization.cpp:67:29: required from here
D:\test_template_initicialization.cpp:16:56: error: incompatible types in assignment of 'long int (*)[4]' to 'long int [4][4]'
Matrix(long int matrix[height][width]) : _data(matrix)
La parte principal del error es'long int (*)[4]' to 'long int [4][4]'
. loslong int (*)[4]
viene de lamatrix2 = { 0 };
y ellong int [4][4]
es mi declaración de plantilla de clase estándarlong int _data[height][width];
.
Puedo arreglar miMatrix
constructor, para que pueda aceptar estolong int (*)[4]
procedente dematrix2 = { 0 };
llamada de inicialización?