如何解决错误:线程 1:EXC_BAD_ACCESS代码=EXC_I386_GPFLT
我正在尝试使用链表来实现稀疏矩阵。
错误显示线程 1: EXC_BAD_ACCESS (code=1,address=0x8) 在以下 c++ 代码中。 错误可能来自访问权限已释放。但我无法调试。
输入(行,列,值)
//输入稀疏矩阵维数。值为非零项。
4 5 6
//当我尝试输入稀疏矩阵时的下一步。
//格式为(row,col,value).
1 2 3
// 错误来自这里。
#include <iostream>
using namespace::std;
struct Triple{
int row;
int col;
int val;
};
class Matrix;
class Matrixnode{
friend class Matrix;
friend istream &operator>>(istream &is,Matrix &matrix);
friend ostream &operator<<(ostream &os,Matrix &matrix);
private:
Matrixnode *right;
Matrixnode *down;
bool head;
union{
Matrixnode* next;
Triple triple;
};
public:
Matrixnode(bool b,Triple*t){
head=b;
if(head){right=down=this;}
else triple=*t;
};
};
class Matrix{
friend istream& operator>>(istream& is,Matrix& matrix);
friend ostream& operator<<(ostream& os,Matrix& matrix);
public:
Matrix(){
}
int max(int a,int b){
if(a>b)
return a;
else
return b;
}
private:
Matrixnode* headnode;
};
istream &operator>>(istream &is,Matrix &matrix)
{
Triple s;
cout<<"input(row,value)"<<endl;
is >> s.row >> s.col >> s.val;
int p=matrix.max(s.row,s.col);
matrix.headnode =new Matrixnode(false,&s);
if(p==0)
{
matrix.headnode->right=matrix.headnode;
return is;
}
Matrixnode **head = new Matrixnode*[p];
for (int i=0;i<p;i++)
head[i]=new Matrixnode(true,0);
int currentRow=0;
Matrixnode *last=head[0];
for(int i=0;i<s.val;i++)
{
Triple t;
is >>t.row>>t.col>>t.val;
if (t.row>currentRow)
{
last->right=head[currentRow];
currentRow =t.row;
last=head[currentRow];
}
last=last->right=new Matrixnode(false,&t);
head[t.col]->next=head[t.col]->next->down=last; \\error!!!!
}
last->right=head[currentRow];
for (int i=0;i<s.col;i++)
head[i]->next->down=head[i];
for (int i=0;i<p-1;i++)
head[i]->next=head[i+1];
head[p-1]->next=matrix.headnode;
matrix.headnode->right=head[0];
delete[]head;
return is;
}
ostream &operator<<(ostream &os,Matrix &matrix){
Matrixnode *headnode=matrix.headnode->right;
while(headnode!=matrix.headnode)
{
for(Matrixnode *cur=headnode->right;cur!=headnode;cur=cur->right)
os<<cur->triple.row<<","<<cur->triple.col<<","<<cur->triple.val<<endl;
headnode=headnode->next;
}
return os;
}
int main(int argc,const char * argv[]) {
Matrix m;
cin >>m;
cout <<m;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。