【问题标题】:How to pass a 2D arrays of pointers to 3D arrays of pointers (function and class) and return 3d arrays of values in C++?如何将 2D 指针数组传递给 3D 指针数组(函数和类)并在 C++ 中返回 3D 值数组?
【发布时间】:2021-12-31 05:06:05
【问题描述】:

我目前正在研究 C++,创建一个矩阵分析程序。 据我所知,可以使用像 array2D[3][3]={{1,2,3},{4,5,6},{7,8,9}} 这样的数组数组来创建它们。

因此,我所做的是在一个类中生成一个函数,这样的函数必须返回一个二维数组。然后我创建了另一个类以生成另一个数组数组,但是这个 3D 数组需要前一个类获得的数据,记住前一个类将矩阵的值保存在一个名为 int **degreesOfFreedom 的变量中。这就是问题出现的地方,第二个类需要双指针的值,就会出现这样的问题。

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

【问题讨论】:

    标签: c++ pointers multidimensional-array


    【解决方案1】:

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

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

    int*** indexes = {};
    

    【讨论】:

    • 非常感谢,现在一切正常。你知道我没有注意到这样的错误。非常感谢您的快速回复
    猜你喜欢
    • 2017-06-29
    • 1970-01-01
    • 2016-06-23
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 2021-12-02
    相关资源
    最近更新 更多