当我尝试为矩阵计算器执行此代码时,我不断遇到运行时错误

时间:2019-12-25 23:53:24

标签: c++

我已经写了一类简单的矩阵计算器,但是每当我尝试添加两个矩阵时,它都会给我运行时错误,请帮忙。这是代码。我不知道问题出在哪里,我猜它不接受任何函数中2D数组的使用,并给出了运行时错误

class Calculator
{
private:
    int row;
    int col;
    int **matrix;
public:
    Calculator(int, int);
    int getrow();
    int getcol();
    int getval(int, int);
    void sum(Calculator);
};

int Calculator::getrow()
{
    return row;
}

int Calculator::getcol()
{
    return col;
}

int Calculator::getval(int i, int j)
{
    return matrix[i][j];
}

void Calculator::sum(Calculator  m)
{
    int **sum = new int*[row];
    for (int i = 0; i < row; i++)
        sum[i] = new int[col];
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            sum[i][j] = this->matrix[i][j] + m.getval(i,j);
        }
    }
}

0 个答案:

没有答案