矩阵类运算符重载,析构函数问题 [英] Matrix class operator overloading,destructor problem

查看:175
本文介绍了矩阵类运算符重载,析构函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个矩阵类,该类可以找到逆,伴随等.任何阶数的方阵 构造函数初始化一个n阶(传递给它)的恒等矩阵.

I was trying to write a matrix class which would be able to find inverse,adjoint,etc. of a square matrix of any order. The constructor initializes an identity matrix of order n(passed to it).

class Matrix
{
int** elements;
int order;

public:
Matrix& operator=(const Matrix& second_inp)
{
    if(this->order!=second_inp.order)
        cout<<"The matrix cannot be assigned!!!\n"<<this->order<<"\n"<<second_inp.order;

    else
    {
        for(int i=0;i<this->order;i++)
            for(int j=0;j<this->order;j++)
                this->elements[i][j] = second_inp.elements[i][j];

    }

    return *this;
}

Matrix operator*(const Matrix& a)const
{
    Matrix c(a.order);

    for(int i=0;i<c.order;i++)                      
        for(int j=0;j<c.order;j++)
            c.elements[i][j]=0;

    if (this->order!=a.order)
    {
        cout<<"The 2 Matrices cannot be multiplied!!!\n";
        return Matrix();
    }

    else
    {
        for(int i=0;i<a.order;i++)
            for(int j=0;j<a.order;j++)
                for(int k=0;k<a.order;k++)
                    c.elements[i][j] += (this->elements[i][k])*(a.elements[k][j]);

        return c;
    }
}
};

~Matrix()
{
    for(int i=0;i<this->order;i++)
        delete[] *(elements+i);
    delete[] elements;
    elements=nullptr;
}

如果我要使用此类运行以下代码:

If i were to run the following code using this class:

Matrix exp1(2),exp2(2),exp3(2);
exp1.get_matrix();
exp3=exp1*exp2;
exp3.show_matrix();

我在调试时遇到运行时错误,发现在乘(exp1 * exp2)之后,如果* operator的结果是= operator无法访问数据.

I get a run-time error, while debugging i found out that, after the multiplication(exp1*exp2) the =operator was not able to access the data if the result of the *operator.

但是,如果我要在main()的末尾使用这样的手动析构函数来释放所有分配的内存,则该程序可以正常工作.

But if i were to use a manual destructor like this one at the end of the main() to free all allocated memory, the program works fine.

void destroctor()
{
  for(int i=0;i<order;i++)
    delete[] *(elements+i);
  delete[] elements;
}

我该如何编辑析构函数或运算符重载来解决此问题?

how can i edit the destructor or the operator overloads to correct this problem?

我使用的构造函数:

Matrix(int inp_order):order(inp_order)
{
    elements=new int*[order];

    for(int i=0;i<order;i++)
        *(elements+i)=new int[order];

    for(int i=0;i<order;i++)
        for(int j=0;j<order;j++)
        {
            if (i==j)
                *(*(elements+j)+i)=1;
            else
                *(*(elements+j)+i)=0;
        }
}

推荐答案

由于您尚未发布构造函数,因此很难说出问题所在.

It is hard to tell what is going wrong, since you have not posted your constructors.

exp3=exp1*exp2;中发生了很多事情:

首先在operator *函数中构造一个新矩阵c.然后return c;语句调用复制构造函数,然后调用析构函数.之后,调用operator =,然后再次调用临时矩阵的析构函数.

First a new matrix c is constructed in the operator* function. Then the return c; statement calls the copy constructor and then the destructor. After that operator= is called and after that the destructor for the temporary matrix again.

我认为发生的事情是您正在使用默认的复制构造函数,该构造函数不会进行深层复制.这样,在return c时调用的析构函数将删除仍在矩阵之间共享的数据.

I think what happens is that you are using the default copy constructor which does not make a deep copy. That way the destructor being called at the time of return c deletes the data that still shared between the matrices.

这篇关于矩阵类运算符重载,析构函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆