C++矩阵类定义,遇到了Segmentation fault

OJ系统一直提示:Segmentation fault:段错误,检查是否有数组越界,指针异常,访问到不应该访问的内存区域

但是一直检查不出来哪里有错误。二维动态数组这个问题困扰我好久了,求指点!

#include
using namespace std;
//矩阵类定义
class Matrix
{
public:
    Matrix(int, int);
    void set(int, int);
    friend istream& operator>>(istream &, Matrix&);
    friend ostream& operator<<(ostream&, const Matrix&);
    Matrix& operator*(Matrix&);
    void DiagonalOrder();
    void setZeros();
    
protected:
    int** mat;                                     //矩阵(二位数组)
    int row;                                        //行数
    int col;                                         //列数
};
Matrix temp(0,0);                               //定义全局对象,用于接受矩阵乘法结果
void Matrix::set(int n, int m)                
{
    row = n;
    col = m;
    mat = new int* [n];
    for (int i = 0; i < n; i++)
    {
        mat[i] = new int[m];
    }
    if (mat == NULL)
    {
        cout << "allocation failure!\n";
        return;
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            mat[i][j] = 0;
        }
    }
}
Matrix::Matrix(int n, int m)                       //构造函数,初始化行数列数与矩阵
{
    row = n;
    col = m;
    if (m > 0 && n > 0)
    {
        mat = new int* [n];
        for (int i = 0; i < n; i++)
        {
            mat[i] = new int[m];
        }
        if (mat == NULL)
        {
            cout << "allocation failure!\n";
            return;
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                mat[i][j] = 0;
            }
        }
    }
    else
        mat = NULL;
}
istream& operator>>(istream& input, Matrix& a)        //重载输入运算符
{
    for (int i = 0; i < a.row; i++)
    {
        for (int j = 0; j < a.col; j++)
        {
            input >> a.mat[i][j];
        }
    }
    return input;
}
ostream& operator<<(ostream& output,const Matrix& a)     //重载输出运算符
{
   
    for (int i = 0; i < a.row; i++)
    {
        for (int j = 0; j < a.col-1; j++)
        {
            output << a.mat[i][j] << " ";
        }
        cout << a.mat[i][a.col - 1] << endl;
    }
    return output;
}

Matrix& Matrix::operator*(Matrix&a)                        //重载矩阵乘法
{
    if (col == a.row)
    {
        temp.set(row, a.col);
        
        for (int i = 0; i < temp.row; i++)
        {
            for (int j = 0; j < temp.col; j++)
            {
                int sum = 0;
                int k = 0,o = 0;
                while (k < col && o < a.row)
                {
                    sum += mat[i][k] * a.mat[o][j];
                    k++; o++;
                }
                
                temp.mat[i][j] = sum;
            }
        }
        return temp;
    }
    else
    {
        cout << "can't multiply!" << endl;
        
    }
}
void Matrix::DiagonalOrder()                                               //之字形打印
{
    
        cout << mat[0][0] << " ";
    
       
        int is_up = 1;
        int x = 0, y = 0;
        while (x >= 0 && x <= row - 1 && y >= 0 && y <= col - 1)
        {
            if (x == 0)
            {
                if (y == 0)
                {
                    y++;
                    is_up = 0;                  
                }
                else if(y-1)
                {
                    if (is_up == 0)
                    {
                        x++; y--;
                        
                    }
                    else
                    {
                       y++;
                       is_up = 0;
                    }
                }
                else
                {
                    is_up = 0;
                    x++;
                }
            }
            else if (x == row - 1)
            {
                if (y>=0&&y-1)
                {
                    if (is_up)
                    {
                        x--; y++;
                    }
                    else
                    {
                        y++;
                        is_up = 1;
                    }
                }
                
            }

            else if (y == 0)
            {
                if (is_up == 1)
                {
                    x--; y++;
                }
                else
                {
                    x++;
                    is_up = 1;
                }
            }
            else if (y == col - 1)
            {
                if (is_up == 1)
                {
                    x++;
                    is_up = 0;
                }
                else
                {
                    x++; y--;
                }
            }
            else
            {
                if (is_up ==1)
                {
                    x--; y++;
                }
                else
                {
                    x++; y--;
                }
            }

            if (x == row - 1 && y == col - 1)
            {
                cout << mat[x][y] << endl;
                break;
            }
            else
                cout << mat[x][y] << ' ';
        }
       
       
    
   

}
void Matrix::setZeros()                                            //将矩阵中有0的行和列全部置0
{
    int** temp = new int* [row];
    for (int i = 0; i < row; i++)
    {
        temp[i] = new int[col];
    }
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            temp[i][j] = mat[i][j];
        }
    }
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            if (temp[i][j] == 0)
            {
                for (int k = 0; k < col; k++)
                    mat[i][k] = 0;
                for (int o = 0; o < row; o++)
                    mat[o][j] = 0;
            }
        }
    }
    delete[]temp;

}

int main() {

    int n, m;

    while (cin >> m >> n) {

        Matrix mat1(m, n);

        cin >> mat1;



        int x, y;

        cin >> x >> y;

        Matrix mat2(x, y);

        cin >> mat2;



        Matrix res = mat1 * mat2;

        cout << res;



        mat1.DiagonalOrder();



        mat1.setZeros();

        cout << mat1;

    }

    return 0;

}

这个问题很有可能是数组越界或者指针引用了无效的内存,你可以尝试在程序中添加一些调试代码,来追踪程序的执行情况。

具体的,你可以使用以下方法来定位错误:

在代码中加入许多 cout 语句,来追踪变量的值,以及程序的执行情况,这有助于你了解程序是如何执行的。

使用断点调试,来检查程序在执行时是否出现错误。

使用 assert 语句,如果一个表达式的结果为 false,则程序将中止执行,这有助于你定位错误。

希望这些建议能帮助你找到问题所在。