我目前正在研究 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;
}
uj5u.com熱心網友回復:
問題是該函式matrixOfIndexes回傳一個 3 維指標 ( int***) 但您分配該回傳值的陣列是一個二維一 ( int**)。型別必須匹配。
要修復它,只需*在以下宣告中添加額外內容indexes:
int*** indexes = {};
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/361748.html
