C++ 矩阵的动态分配使程序无法响应
我已经浏览了大多数已经发布的问题,似乎找不到解决方法:在指针指向某个对象之前,不能取消引用指针<代码>永远不会初始化,但您可以执行以下操作:C++ 矩阵的动态分配使程序无法响应,c++,dynamic,matrix,allocation,C++,Dynamic,Matrix,Allocation,我已经浏览了大多数已经发布的问题,似乎找不到解决方法:在指针指向某个对象之前,不能取消引用指针永远不会初始化,但您可以执行以下操作: *A = new int[n]; 此(*A)正在取消引用。在取消引用之前,您需要将A设置为指向某个对象。我在帖子中说过,n是9。是的。您忘记分配A。这是一个非常非常规的>运算符。这就是AllocA()应该做的。。。>>操作员不是问题所在。这是一个静态矩阵。Alloca中的行应该是*a=newint*[n]@Radvo*A=newint[n]相当于A[0]=new
*A = new int[n];
此(
*A
)正在取消引用。在取消引用之前,您需要将A
设置为指向某个对象。我在帖子中说过,n是9。是的。您忘记分配A
。这是一个非常非常规的>
运算符。这就是AllocA()应该做的。。。>>操作员不是问题所在。这是一个静态矩阵。Alloca中的行应该是*a=newint*[n]@Radvo*A=newint[n]
相当于A[0]=newint[n]
。您需要A=newint*[n]
。
int main(){
Graph graphA;
"input.txt">>graphA;
}
9
0 1 1 0 0 0 0 0 0
1 0 0 1 1 0 0 0 0
1 0 0 0 0 0 0 1 1
0 1 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0 0
0 0 0 0 1 0 1 0 0
0 0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
void operator>>(char *fis_in,Graph &graph){
int k,x;
ifstream fin(fis_in);
fin>>k;
graph.Setn(k);
graph.AllocA();
int i,j;
for(i=0;i<k;i++)
for(j=0;j<k;j++){
fin>>x;
graph.SetA(i,j,x);}
cls; // #define cls system("cls"), just for convenience
cout<<"Done !";
delay(1); // Irrelevant, just a 1 second delay
fin.close();
}
void Graph::AllocA(){
int i;
*A = new int[n];
for(i=0;i<n;i++)
A[i] = new int[n];}
*A = new int[n];