繁体   English   中英

How to pass a 2D arrays of pointers to 3D arrays of pointers (function and class) and return 3d arrays of values in C++?

[英]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++,创建一个矩阵分析程序。 据我所见,可以使用 arrays 的 arrays 像这样的array2D[3][3]={{1,2,3},{4,5,6},{7,8,9}}来创建这些。

因此,我所做的是在 class 中生成 function,这样的 function 必须返回一个二维数组。 Then i created another class in order to generate the other array of array of arrays, but this 3D arrays need the data obtained by the previous class, remember the previous class holds the values of the matrix in a variable called int **degreesOfFreedom . 这就是问题出现的地方,第二个 class 需要双指针的值,就会出现这样的问题。

error: cannot convert ‘int***’ to ‘int**’ in assignment

据我所见,尝试将二维指针 arrays 传递到 3D 指针 function 时出现错误。

顺便说一句,我已经检查了几种方法,我看到其中一种方法是替换 function 内部变量声明中的双指针** ,并将其替换为[][] 我已经尝试过了,它没有解决问题,我也不想那样做,因为将来我将拥有每百万个元素的百万个矩阵。

如果有人可以帮助我或通过正确的地址与我联系会很好

提前致谢

这是我的代码

#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;
}

问题是 function matrixOfIndexes返回一个 3 维指针 ( int*** ) 但您分配该返回值的数组是一个二维的 ( int** )。 类型必须匹配。

要修复它,只需在indexes声明中添加一个额外的*

int*** indexes = {};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM