多维对象数组c ++ - 如何?

时间:2021-09-01 21:34:35

if had some troubles finding how to exactly do it, so i decided to touch and go on the problem, and it seems a have it working now.

如果有一些麻烦找到如何做到这一点,所以我决定触摸并继续解决这个问题,现在似乎已经有了它。

The code is:

代码是:

Flight ***Matr = new Flight**[airportCount];
for(int i = 0; i < airportCount; i++){
    Matr[i] = new Flight*[airportCount];
    for(int j = 0; j < airportCount; j++){
        Matr[i][j] = new Flight;
    }
}
for(int i = 0; i < airportCount; i++){
    for (int j = 1; j < airportCount; j++){
        Matr[i][j];
    }
    delete[] Matr[i];
}
delete[] Matr;

and when i try to delete Matr[0][0] is sais segmentation error; yay

当我尝试删除Matr [0] [0]时,sais分段错误;好极了

any errors i've missed?

我错过了什么错误?

2 个解决方案

#1


1  

change line Matr[i][j] = new Flight; to Matr[i][j] = new Flight[len]; and change line Matr[i][j]; to delete[] Matr[i][j];

改变线Matr [i] [j] =新的飞行;到Matr [i] [j] =新航班[len];并改变行Matr [i] [j];删除[] Matr [i] [j];

#2


2  

why not using vectors?

为什么不使用矢量?

vector < vector < Flight > > Matr( airportCount,
                                   vector < Flight >( airportCount ) )

#1


1  

change line Matr[i][j] = new Flight; to Matr[i][j] = new Flight[len]; and change line Matr[i][j]; to delete[] Matr[i][j];

改变线Matr [i] [j] =新的飞行;到Matr [i] [j] =新航班[len];并改变行Matr [i] [j];删除[] Matr [i] [j];

#2


2  

why not using vectors?

为什么不使用矢量?

vector < vector < Flight > > Matr( airportCount,
                                   vector < Flight >( airportCount ) )