简体   繁体   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++?

I am currently working on C++, creating a program of the analysis of matrices.我目前正在研究 C++,创建一个矩阵分析程序。 As far as i saw those can be created using an arrays of arrays like this array2D[3][3]={{1,2,3},{4,5,6},{7,8,9}} .据我所见,可以使用 arrays 的 arrays 像这样的array2D[3][3]={{1,2,3},{4,5,6},{7,8,9}}来创建这些。

Therefore, what i did was to generate a function in a class, such a function must return a 2D array.因此,我所做的是在 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 . 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 . Here is where the problem arises this second class needs the values of the double pointer and the problem like this appears.这就是问题出现的地方,第二个 class 需要双指针的值,就会出现这样的问题。

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

As far as i can see the error comes when trying to pass the 2D pointers arrays into the 3D pointers function.据我所见,尝试将二维指针 arrays 传递到 3D 指针 function 时出现错误。

By the way i already checked several ways and i saw that one of them is replacing the double pointer ** in the declaration of the variable inside of the function, and replace it by [][] .顺便说一句,我已经检查了几种方法,我看到其中一种方法是替换 function 内部变量声明中的双指针** ,并将其替换为[][] I already tried that, it didn't solve the problem, also i would not like to do that way because in the future i will have matrices of millions per millions elements.我已经尝试过了,它没有解决问题,我也不想那样做,因为将来我将拥有每百万个元素的百万个矩阵。

If someone can help me or address me through the right will be nice如果有人可以帮助我或通过正确的地址与我联系会很好

Thanks in advance提前致谢

This is my code这是我的代码

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

The problem is that the function matrixOfIndexes returns a 3-dimensional pointer ( int*** ) but the array you're assigning that return value to is a two-dimensional one ( int** ).问题是 function matrixOfIndexes返回一个 3 维指针 ( int*** ) 但您分配该返回值的数组是一个二维的 ( int** )。 The types have to match.类型必须匹配。

To fix it just add an extra * to the declaration of indexes :要修复它,只需在indexes声明中添加一个额外的*

int*** indexes = {};

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

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