簡體   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