将二维数组传递给函数/构造函数
Passing a 2D array to a function / constructor
无法将 float[3][3] 转换为 float **
我正在编写一个处理矩阵的程序作为学习练习
但是我遇到错误,无法将 float matrix[3][3]
分配给 float**
我知道 2D array
是指针的指针,所以 float **
我这个项目的目标是编写一个简单的 Matrix
class 来创建矩阵并进行一些基本的矩阵运算,例如加法和乘法。
我收到以下错误:
In file included from main.cpp:2:0:
./matrix.h:12:5: note: candidate: Matrix::Matrix(int, int, float**)
Matrix(int, int, float **elements);
^~~~~~
./matrix.h:12:5: note: no known conversion for argument 3 from
‘float [3][3]’ to ‘float**’
我知道从 float[3][3]
更改参数类型可以解决问题。但是矩阵的大小不是固定的,这就是为什么我选择 float**
这是我的代码的一部分:
main.cpp
#include"Matrix.h"
int main()
{
float Identity[3][3] =
{
{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}
};
Matrix identity = Matrix(3, 3, Identity);
}
Matrix.h
class Matrix
{
private
MatrixData _matrix = MatrixData();
Matrix(int rows, int columns, float **elements)
{
_matrix.rows = rows;
_matrix.columns = columns;
_matrix.elements = new float[rows * columns];
for(int i = 0; i < rows; i++)
for(int j = 0; j < columns; j++)
_matrix.elements[(i * columns) + j] = elements[i][j];
}
}
MatrixData.h
struct MatrixData
{
unsigned char rows;
unsigned char columns;
float *elements;
};
在 MatrixData class
中,元素存储为连续数组。
要创建一个新矩阵,必须将 2D array
传递给 class constructor
。
如果构造函数参数类型是float *
我在构造函数中传递 Identity[0]
,它起作用了。
我想要的是通过 Identity
而不是像 Identity[0]
或 &Identity[0][0]
.
这样的东西
谁能告诉我有什么解决办法。
谢谢。
二维数组是连续存储的,所以你应该像这个例子那样做:
#include <iostream>
template<size_t N1, size_t N2>
void output(const float (&elements)[N1][N2])
{
for (size_t i = 0; i < N1; i++)
{
for (size_t j = 0; j < N2; j++)
std::cout << elements[i][j] << ' ';
std::cout << std::endl;
}
}
int main()
{
float elements[3][3] = {
{1,2,3},
{1,2,3},
{1,2,3}
};
output<3, 3>(elements);
}
请注意我是如何使用模板的,因为数组大小是静态的,这是你犯的另一个错误
编辑:我明白你想做什么,方法如下:
将矩阵 class 设为模板 class 获取模板参数行和列,并将 elements
参数更改为 float elements[rows][columns]
template<size_t rows, size_t columns>
class Matrix
{
private:
MatrixData _matrix = MatrixData();
public:
Matrix(float elements[rows][columns])
{
_matrix.rows = rows;
_matrix.columns = columns;
_matrix.elements = new float[rows * columns];
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
_matrix.elements[(i * columns) + j] = elements[i][j];
}
};
然后您必须删除行和列构造函数参数,因为您已经将它们作为模板参数。
现在对象声明应该如下所示:
int main()
{
float Identity[3][3] =
{
{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}
};
Matrix<3, 3> identity(Identity);
}
无法将 float[3][3] 转换为 float **
我正在编写一个处理矩阵的程序作为学习练习
但是我遇到错误,无法将 float matrix[3][3]
分配给 float**
我知道 2D array
是指针的指针,所以 float **
我这个项目的目标是编写一个简单的 Matrix
class 来创建矩阵并进行一些基本的矩阵运算,例如加法和乘法。
我收到以下错误:
In file included from main.cpp:2:0:
./matrix.h:12:5: note: candidate: Matrix::Matrix(int, int, float**)
Matrix(int, int, float **elements);
^~~~~~
./matrix.h:12:5: note: no known conversion for argument 3 from
‘float [3][3]’ to ‘float**’
我知道从 float[3][3]
更改参数类型可以解决问题。但是矩阵的大小不是固定的,这就是为什么我选择 float**
这是我的代码的一部分:
main.cpp
#include"Matrix.h"
int main()
{
float Identity[3][3] =
{
{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}
};
Matrix identity = Matrix(3, 3, Identity);
}
Matrix.h
class Matrix
{
private
MatrixData _matrix = MatrixData();
Matrix(int rows, int columns, float **elements)
{
_matrix.rows = rows;
_matrix.columns = columns;
_matrix.elements = new float[rows * columns];
for(int i = 0; i < rows; i++)
for(int j = 0; j < columns; j++)
_matrix.elements[(i * columns) + j] = elements[i][j];
}
}
MatrixData.h
struct MatrixData
{
unsigned char rows;
unsigned char columns;
float *elements;
};
在 MatrixData class
中,元素存储为连续数组。
要创建一个新矩阵,必须将 2D array
传递给 class constructor
。
如果构造函数参数类型是float *
我在构造函数中传递 Identity[0]
,它起作用了。
我想要的是通过 Identity
而不是像 Identity[0]
或 &Identity[0][0]
.
谁能告诉我有什么解决办法。
谢谢。
二维数组是连续存储的,所以你应该像这个例子那样做:
#include <iostream>
template<size_t N1, size_t N2>
void output(const float (&elements)[N1][N2])
{
for (size_t i = 0; i < N1; i++)
{
for (size_t j = 0; j < N2; j++)
std::cout << elements[i][j] << ' ';
std::cout << std::endl;
}
}
int main()
{
float elements[3][3] = {
{1,2,3},
{1,2,3},
{1,2,3}
};
output<3, 3>(elements);
}
请注意我是如何使用模板的,因为数组大小是静态的,这是你犯的另一个错误
编辑:我明白你想做什么,方法如下:
将矩阵 class 设为模板 class 获取模板参数行和列,并将 elements
参数更改为 float elements[rows][columns]
template<size_t rows, size_t columns>
class Matrix
{
private:
MatrixData _matrix = MatrixData();
public:
Matrix(float elements[rows][columns])
{
_matrix.rows = rows;
_matrix.columns = columns;
_matrix.elements = new float[rows * columns];
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
_matrix.elements[(i * columns) + j] = elements[i][j];
}
};
然后您必须删除行和列构造函数参数,因为您已经将它们作为模板参数。
现在对象声明应该如下所示:
int main()
{
float Identity[3][3] =
{
{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}
};
Matrix<3, 3> identity(Identity);
}