Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误:线程1:EXC\U错误\U访问(代码=EXC\U I386\U GPFLT)_C++_Exc Bad Access - Fatal编程技术网

C++ 错误:线程1:EXC\U错误\U访问(代码=EXC\U I386\U GPFLT)

C++ 错误:线程1:EXC\U错误\U访问(代码=EXC\U I386\U GPFLT),c++,exc-bad-access,C++,Exc Bad Access,我正在尝试使用链表实现稀疏矩阵 错误显示,在下面的C++代码中,线程1:ExcBADIAccess(代码=1,地址=0x8)。 错误可能来自访问已释放的访问。但我无法调试它 该守则的用途如下: 输入(行、列、值) //输入稀疏矩阵维度。值为非零项。 4.56 //下一步,当我尝试输入稀疏矩阵时。 //格式为(行、列、值)。 1 2 3 //错误来自这里 #include <iostream> using namespace::std; struct Triple{ int

我正在尝试使用链表实现稀疏矩阵

<>错误显示,在下面的C++代码中,线程1:ExcBADIAccess(代码=1,地址=0x8)。 错误可能来自访问已释放的访问。但我无法调试它

该守则的用途如下:

输入(行、列、值)
//输入稀疏矩阵维度。值为非零项。
4.56
//下一步,当我尝试输入稀疏矩阵时。
//格式为(行、列、值)。
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,col,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;
}
#包括
使用namespace::std;
结构三元组{
int行;
int col;
int-val;
};
类矩阵;
类矩阵节点{
友元类矩阵;
friend istream&operator>>(istream&is,Matrix&Matrix);
friend ostream&operator(istream&is,Matrix&Matrix);
friend ostream&operator>(istream&is,Matrix&Matrix)
{
三s;
cout s.col>>s.val;
INTP=矩阵最大值(s行,s列);
matrix.headnode=新的Matrixnode(false,&s);
如果(p==0)
{
matrix.headnode->right=matrix.headnode;
回报是;
}
Matrixnode**head=新Matrixnode*[p];
对于(int i=0;it.row>>t.col>>t.val;
如果(t.row>currentRow)
{
最后->右=头部[当前行];
currentRow=t.row;
last=头[当前行];
}
last=last->right=newmatrixnode(false,&t);
head[t.col]->next=head[t.col]->next->down=last;\\error!!!!
}
最后->右=头部[当前行];
对于(int i=0;不精确->向下=头部[i];
对于(整数i=0;整数i=head[i+1];
head[p-1]->next=matrix.headnode;
矩阵.头节点->右=头[0];
删除[]标题;
回报是;
}
ostream&operatorright;cur!=headnode;cur=cur->right)

osUnion???为什么?这是在问问题。为什么矩阵类中有一个max?是不是
std::max
不好?@JHBonarius矩阵类中有一个max的原因是因为istream需要使用。@Henry你的
Matrixnode
构造函数需要设置所有成员变量(即
right
down
),无论
if
条件如何。即使您必须将这些指针设置为
nullptr
,也需要设置它们。您是否在任何地方设置next而不使用next(也应初始化):head[t.col]->next=head[t.col]->next->down=last;\\error!!!