如何在 C++ 中将 2D 指针数组传递给 3D 指针数组(函数和 class)和 return 3d 值数组?
How to pass a 2D arrays of pointers to 3D arrays of pointers (function and class) and return 3d arrays of values in C++?
我目前正在研究 C++,创建一个矩阵分析程序。
据我所知,可以使用这样的数组数组创建这些 array2D[3][3]={{1,2,3},{4,5,6},{7,8,9}}
.
因此,我所做的是在class中生成一个函数,这样的函数必须return一个二维数组。然后我创建了另一个 class 以生成数组数组的另一个数组,但是这个 3D 数组需要前面 class 获得的数据,记住前面的 class 保存的值名为 int **degreesOfFreedom
的变量中的矩阵。第二次出现问题的地方class需要双指针的值,出现这样的问题。
error: cannot convert ‘int***’ to ‘int**’ in assignment
据我所知,在尝试将 2D 指针数组传递到 3D 指针函数时出现错误。
顺便说一句,我已经检查了几种方法,我看到其中一种是在函数内部变量声明中替换双指针 **
,并将其替换为 [][]
.我已经试过了,它没有解决问题,我也不想那样做,因为将来我会有百万分之一的矩阵。
这是我的代码
#include <iostream>
#include <string>
#include <fstream>
class MatrixOfDegreesOfFreedom
{
public:
int X, Y;
int M = 0;
public:
int **matrixOfDegreesOfFreedom(int rows, int cols)
{
X = rows;
Y = cols;
int** matrix = new int*[X];
for (int i = 0; i < X; ++i)
{
matrix[i] = new int[Y];
for (int j = 0; j < Y; ++j)
{
matrix[i][j] = M;
M = M + 1;
}
}
return matrix;
}
//constructor
MatrixOfDegreesOfFreedom()
{
}
//destructor
~MatrixOfDegreesOfFreedom()
{
}
};
class MatrixOfIndexes
{
public:
int X, Y, Z;
int M = 0;
public:
int ***matrixOfIndexes(int rows, int cols, int colsTwo, int conect[][2], int **DoF)
{
X = rows;
Y = cols;
Z = colsTwo;
int*** matrix = new int**[X];
for (int i = 0; i < X; ++i)
{
M = 0;
matrix[i] = new int*[Y];
for (int j = 0; j < Y; ++j)
{
matrix[i][j] = new int [Z];
for (int t = 0; t < Z; ++t)
{
matrix[i][j][t] = DoF[conect[i][j]][t];
}
M = M + 1;
}
}
return matrix;
}
//constructor
MatrixOfIndexes()
{
}
//destructor
~MatrixOfIndexes()
{
}
};
int main(int argc, char const *argv[])
{
#ifndef OUTPUT
freopen("output.txt", "w", stdout); // file to store the output data.
#endif
int numberOfNodes = 3; // number of nodes
int numberOfDegreesOfFreedomPerNode = 2; //Number of Degrees of Freedom per node
int **degreesOfFreedom = {}; //number of degree of freedom
int numberOfDegreesOfFreedomPerElement = 4; //Number of Degrees of Freedom per element
int numberOfElements = 3;
int connectivity[numberOfElements][2] = {{0,1},{2,1},{0,2}}; // Conectivity matrix along with the property
int **indexes = {};
MatrixOfDegreesOfFreedom tableOfDegreesOfFreedom;
degreesOfFreedom = tableOfDegreesOfFreedom.matrixOfDegreesOfFreedom(numberOfNodes, numberOfDegreesOfFreedomPerNode);
MatrixOfIndexes tableOfIndexes;
indexes = tableOfIndexes.matrixOfIndexes(numberOfElements, numberOfDegreesOfFreedomPerElement, numberOfDegreesOfFreedomPerNode, connectivity, degreesOfFreedom);
std::cout<< "finishing" << std::endl;
return 0;
}
问题是函数 matrixOfIndexes
return 是一个 3 维指针 (int***
),但是您要为其分配 return 值的数组是一个二维一 (int**
)。类型必须匹配。
要修复它,只需在 indexes
的声明中添加一个额外的 *
:
int*** indexes = {};
我目前正在研究 C++,创建一个矩阵分析程序。
据我所知,可以使用这样的数组数组创建这些 array2D[3][3]={{1,2,3},{4,5,6},{7,8,9}}
.
因此,我所做的是在class中生成一个函数,这样的函数必须return一个二维数组。然后我创建了另一个 class 以生成数组数组的另一个数组,但是这个 3D 数组需要前面 class 获得的数据,记住前面的 class 保存的值名为 int **degreesOfFreedom
的变量中的矩阵。第二次出现问题的地方class需要双指针的值,出现这样的问题。
error: cannot convert ‘int***’ to ‘int**’ in assignment
据我所知,在尝试将 2D 指针数组传递到 3D 指针函数时出现错误。
顺便说一句,我已经检查了几种方法,我看到其中一种是在函数内部变量声明中替换双指针 **
,并将其替换为 [][]
.我已经试过了,它没有解决问题,我也不想那样做,因为将来我会有百万分之一的矩阵。
这是我的代码
#include <iostream>
#include <string>
#include <fstream>
class MatrixOfDegreesOfFreedom
{
public:
int X, Y;
int M = 0;
public:
int **matrixOfDegreesOfFreedom(int rows, int cols)
{
X = rows;
Y = cols;
int** matrix = new int*[X];
for (int i = 0; i < X; ++i)
{
matrix[i] = new int[Y];
for (int j = 0; j < Y; ++j)
{
matrix[i][j] = M;
M = M + 1;
}
}
return matrix;
}
//constructor
MatrixOfDegreesOfFreedom()
{
}
//destructor
~MatrixOfDegreesOfFreedom()
{
}
};
class MatrixOfIndexes
{
public:
int X, Y, Z;
int M = 0;
public:
int ***matrixOfIndexes(int rows, int cols, int colsTwo, int conect[][2], int **DoF)
{
X = rows;
Y = cols;
Z = colsTwo;
int*** matrix = new int**[X];
for (int i = 0; i < X; ++i)
{
M = 0;
matrix[i] = new int*[Y];
for (int j = 0; j < Y; ++j)
{
matrix[i][j] = new int [Z];
for (int t = 0; t < Z; ++t)
{
matrix[i][j][t] = DoF[conect[i][j]][t];
}
M = M + 1;
}
}
return matrix;
}
//constructor
MatrixOfIndexes()
{
}
//destructor
~MatrixOfIndexes()
{
}
};
int main(int argc, char const *argv[])
{
#ifndef OUTPUT
freopen("output.txt", "w", stdout); // file to store the output data.
#endif
int numberOfNodes = 3; // number of nodes
int numberOfDegreesOfFreedomPerNode = 2; //Number of Degrees of Freedom per node
int **degreesOfFreedom = {}; //number of degree of freedom
int numberOfDegreesOfFreedomPerElement = 4; //Number of Degrees of Freedom per element
int numberOfElements = 3;
int connectivity[numberOfElements][2] = {{0,1},{2,1},{0,2}}; // Conectivity matrix along with the property
int **indexes = {};
MatrixOfDegreesOfFreedom tableOfDegreesOfFreedom;
degreesOfFreedom = tableOfDegreesOfFreedom.matrixOfDegreesOfFreedom(numberOfNodes, numberOfDegreesOfFreedomPerNode);
MatrixOfIndexes tableOfIndexes;
indexes = tableOfIndexes.matrixOfIndexes(numberOfElements, numberOfDegreesOfFreedomPerElement, numberOfDegreesOfFreedomPerNode, connectivity, degreesOfFreedom);
std::cout<< "finishing" << std::endl;
return 0;
}
问题是函数 matrixOfIndexes
return 是一个 3 维指针 (int***
),但是您要为其分配 return 值的数组是一个二维一 (int**
)。类型必须匹配。
要修复它,只需在 indexes
的声明中添加一个额外的 *
:
int*** indexes = {};