鱼C论坛

 找回密码
 立即注册
查看: 1225|回复: 4

[已解决]关于析构函数和运算符重载问题

[复制链接]
发表于 2019-5-7 20:01:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目:定义两个矩阵对象,用运算符重载运算矩阵的加法和减法
我打了这样的代码,但是卡在了析构函数上面
#include <iostream>
using namespace std;
class Matrix
{
private:
        double* mat;
        int x;//行
        int y;//列
public:
        int Getx()
        {
                return x;
        }
        int Gety()
        {
                return y;
        }
        Matrix(int x, int y);
        friend Matrix operator + (Matrix& m_a, Matrix& m_b);//矩阵加法;
        friend Matrix operator - (Matrix m_a, Matrix m_b);//矩阵减法;
        Matrix& operator = (const Matrix m);
        void SetMatrix();//设置矩阵的大小
        void Diapaly();
        ~Matrix();
};

#include <iomanip>
void Matrix::SetMatrix()
{
        for (int i = 0; i < x; i++)
        {
                for (int j = 0; j < y; j++)
                {
                        cout << i + 1 << "行," << j + 1 << "列 : ";
                        cin >> *(mat + y * i + j);
                }
                cout << "\n";
        }
}
Matrix::Matrix(int m, int n)
{
        x = m;
        y = n;
        mat = new double[x * y];
}
Matrix::~Matrix()
{
        if (mat)
        {
                delete[]mat;//[b]如果把这个注释掉就可以正常运行[/b]
        }
}
Matrix operator+(Matrix & m_a, Matrix & m_b)//加法
{
        Matrix ma(m_a.x, m_a.y);
        for (int i = 0; i < m_a.x * ma.y; i++)
        {
                ma.mat[i] = m_a.mat[i] + m_b.mat[i];
        }
        return ma;
}
Matrix operator-(Matrix m_a, Matrix m_b)//减法
{
        Matrix ma(m_a.x, m_a.y);
        for (int i = 0; i < m_a.x * ma.y; i++)
        {
                ma.mat[i] = m_a.mat[i] - m_b.mat[i];
        }
        return ma;
}

Matrix & Matrix::operator= (Matrix m)
{
        mat = new double[m.x * m.y];
        for (int i = 0; i < m.x * m.y; i++)
        {
                mat[i] = m.mat[i];
        }
        return *this;
}
void Matrix::Diapaly()
{
        for (int i = 0; i < x; i++)
        {
                for (int j = 0; j < y; j++)
                {
                        cout << setw(5) << *(mat + y * i + j) << " ";
                }
                cout << "\n";
        }
}
int main()
{
        int m, n;
        int flag;
        cout << "A矩阵:" << endl;
        cout << "请输入矩阵行数:";
        cin >> m;
        cout << "请输入矩阵列数:";
        cin >> n;
        Matrix A(m, n);
        A.SetMatrix();
        A.Diapaly();

        cout << "=============\n";
        cout << "B矩阵:" << endl;
        cout << "请输入矩阵行数:";
        cin >> m;
        cout << "请输入矩阵列数:";
        cin >> n;
        Matrix B(m, n);
        B.SetMatrix();
        B.Diapaly();
        cout << "==录入完毕!!==";
        do
        {
                int count = 0;
                cout << "请输入要进行的操作:" << endl;
                cout << "====【1】矩阵相加======" << endl;
                cout << "====【2】矩阵减法======" << endl;
                cout << "请输入相应的序号:" << endl;
                cin >> flag;
                switch (flag)
                {
                case 1:
                {
                        if ((A.Getx() == B.Getx()) && (A.Gety() == B.Gety()))
                        {
                                Matrix C(A.Getx(), B.Gety());
                                C = A + B;
                                cout << "结果: " << endl;
                                C.Diapaly();
                        }
                        else
                        {
                                cout << "\n======行列不相等无法相加=====\n" << endl;
                                break;
                        }
                        break;
                }
                case 2:
                {
                        if ((A.Getx() == B.Getx()) && (A.Gety() == B.Gety()))
                        {
                                Matrix C(A.Getx(), B.Gety());
                                cout << "done" << endl;//测试
                                C = A - B;
                                cout << "结果: " << endl;
                                C.Diapaly();
                        }
                        else
                        {
                                cout << "\n======行列不相等无法相减=====\n" << endl;
                                break;
                        }
                        break;
                }
                default:
                        cout << "请输入正确的序号!!" << endl;
                        break;
                }
        } while (flag != 0);
        return 0;
}
如果我把析构函数的释放内存那行注释掉就可以正常运行
但是不注释就会报错:
批注 2019-05-07 194846.png
问一下这样的错误是什么原因,
应该怎样才能让析构函数正常运行;
最佳答案
2019-5-7 21:50:46
楼主的问题是没有复制构造函数
没有复制构造函数那么默认的复制方案是浅拷贝,也就是拷贝前后的两个矩阵的mat是指向同一个内存块的,前一个析构时释放了这个内存块,后一个析构时重复释放就会导致错误

复制构造函数:
        Matrix(const Matrix& m2):Matrix(m2.x,m2.y) {
                mat = new double[x*y];
                memcpy(mat, m2.mat, sizeof(double)*x*y);
        }

另外,你operator=的重载函数是会导致泄漏的,重新申请空间之前应该删除之前的空间:
Matrix & Matrix::operator= (Matrix m)
{
        x = m.x;
        y = m.y;
        mat = (double*)realloc(mat, sizeof(double)*x*y);
        for (int i = 0; i < m.x * m.y; i++)
        {
                mat[i] = m.mat[i];
        }
        return *this;
}

然后,还有+ -的重载一般来说,型参应该是const Matrix&的,你这样的型参会导致额外的计算负担,

再提一点,作为c++,最好加上移动构造函数,能进一步减少无用功
        Matrix(Matrix&& m2) :Matrix(m2.x, m2.y) {
                mat = m2.mat;
                m2.x = 0;
                m2.y = 0;
                m2.mat = nullptr;
        }
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-5-7 21:50:46 | 显示全部楼层    本楼为最佳答案   
楼主的问题是没有复制构造函数
没有复制构造函数那么默认的复制方案是浅拷贝,也就是拷贝前后的两个矩阵的mat是指向同一个内存块的,前一个析构时释放了这个内存块,后一个析构时重复释放就会导致错误

复制构造函数:
        Matrix(const Matrix& m2):Matrix(m2.x,m2.y) {
                mat = new double[x*y];
                memcpy(mat, m2.mat, sizeof(double)*x*y);
        }

另外,你operator=的重载函数是会导致泄漏的,重新申请空间之前应该删除之前的空间:
Matrix & Matrix::operator= (Matrix m)
{
        x = m.x;
        y = m.y;
        mat = (double*)realloc(mat, sizeof(double)*x*y);
        for (int i = 0; i < m.x * m.y; i++)
        {
                mat[i] = m.mat[i];
        }
        return *this;
}

然后,还有+ -的重载一般来说,型参应该是const Matrix&的,你这样的型参会导致额外的计算负担,

再提一点,作为c++,最好加上移动构造函数,能进一步减少无用功
        Matrix(Matrix&& m2) :Matrix(m2.x, m2.y) {
                mat = m2.mat;
                m2.x = 0;
                m2.y = 0;
                m2.mat = nullptr;
        }
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-7 21:52:57 | 显示全部楼层
本帖最后由 Croper 于 2019-5-7 21:55 编辑

另外,正好我也写过矩阵类,分享一下,
我直接使用的模板管理长宽,内部就是个二维数组,这样就不需要复制构造函数和析构函数了
//Matrix.h
//矩阵类的实现
//Created by Croper,lasted modifered at Apr.13.2019

#ifndef _MATRIX_H_
#define _MATRIX_H_

#include <initializer_list>
#include <iostream>


template <int _cy, int _cx> class Matrix {
        int _data[_cy][_cx];
public:
///////////////////////////////////////////////////////////////////////////
//                            函数声明
///////////////////////////////////////////////////////////////////////////

//--------------------------构造函数----------------------------
        //默认构造函数;
        Matrix();
        //使用初始化列表进行构造,直接一行一行输入,行间不需要使用分隔符;
        Matrix(const std::initializer_list<int>&);
        //复制构造函数
        Matrix(const Matrix&) = default;

        
//-------------------==----运算符重载--------------------------
        // 重载下标运算,以实现矩阵的双下标(a[i][j])写法
        int* operator[](int rowindex);   
        //const版本
        const int* operator[](int rowindex) const;

        //重载*运算,实现矩阵乘法
        template <int _cz> Matrix<_cy, _cz> operator*(const Matrix<_cx, _cz>& m2);


        //-------------------------其他函数------------------------------
        // 矩阵的转置
        Matrix<_cx, _cy> turn();

        // 于控制台打印自身
        void print();
};

///////////////////////////////////////////////////////////////////////////
//                            函数实现
///////////////////////////////////////////////////////////////////////////

//默认构造函数;
template<int _cy, int _cx>
inline Matrix<_cy, _cx>::Matrix() {
        memset(_data, 0, sizeof(_data));
}

//使用初始化列表进行构造,直接一行一行输入,行间不需要使用分隔符;
template<int _cy, int _cx>
inline Matrix<_cy, _cx>::Matrix(const std::initializer_list<int>& list) :Matrix() {
        int *p = (int*)_data;
        for (auto it = list.begin(); it != list.end(); ++it, ++p) {
                *p = *it;
        }
}

// 重载下标运算,以实现矩阵的双下标(a[i][j])写法
template<int _cy, int _cx>
inline int* Matrix<_cy, _cx>::operator[](int rowindex) {
        return _data[rowindex];
}

//const版本
template<int _cy, int _cx>
inline const int* Matrix<_cy, _cx>::operator[](int rowindex) const {
        return const_cast<Matrix*>(this)->operator[](rowindex);
}

//重载*运算,实现矩阵乘法
template<int _cy, int _cx>
template <int _cz>
Matrix<_cy, _cz> Matrix<_cy, _cx>::operator*(const Matrix<_cx, _cz>& m2) {
        Matrix<_cy, _cz> ret;
        for (int y = 0; y < _cy; ++y) for (int z = 0; z < _cz; ++z) {
                for (int x = 0; x < _cx; ++x) {
                        ret[y][z] += _data[y][x] * m2[x][z];
                }
        }
        return ret;
}

// 矩阵的转置
template<int _cy, int _cx>
Matrix<_cx, _cy> Matrix<_cy, _cx>::turn() {
        Matrix<_cx, _cy> ret;
        for (int i = 0; i < _cx; ++i) {
                for (int j = 0; j < _cy; ++j) {
                        ret[i][j] = _data[j][i];
                }
        }
        return ret;
}

// 打印自身
template<int _cy, int _cx>
void Matrix<_cy, _cx>::print() {
        std::cout << std::endl;
        for (int y = 0; y < _cy; ++y) {
                for (int x = 0; x < _cx; ++x) {
                        std::cout << '\t' << _data[y][x];
                }
                std::cout << std::endl;
        }
}

#endif
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-8 07:28:03 | 显示全部楼层
感谢
不过我还有一些不懂:
我只进行了运算符重载,为什么会调用拷贝构造函数?
我确定是运行到重载时出现的问题
是不是在这个函数中我定义了一个新的对象从而调用拷贝构造函数的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-8 14:03:13 | 显示全部楼层
本帖最后由 Croper 于 2019-5-8 14:07 编辑
C = A + B;
我们来分析一下这一句代码发生了什么
首先,A和B作为实参,调用operator+
Matrix operator+(Matrix & m_a, Matrix & m_b)//加法
{
        Matrix ma(m_a.x, m_a.y);
        for (int i = 0; i < m_a.x * ma.y; i++)
        {
                ma.mat[i] = m_a.mat[i] + m_b.mat[i];
        }
        return ma;
}

(顺便一提,这里A和B传递的引用,不会调用复制构造函数。但是你的operator-的参数形式直接是Matrix,那么函数里的形参是A,B的复制,是调用了复制构造函数的)
在函数内部,新建了一个局部变量ma

函数结束,返回ma,
到这时,都没有调用复制构造函数

接下来就要注意了,
首先,系统会产生一个临时的Matrix变量(后面就叫它_tmp吧),调用复制构造函数,复制ma。
然后operator+函数结束,释放空间,ma作为局部变量同时也被析构

然后执行C=tmp,调用operator=(其实准确地说应该是调用的operator=(Matrix&&),移动赋值函数,因为这里的tmp是将亡值,愿意学习的话可以去搜索一下关于左值,右值,纯右值,将亡值的定义,但是没有这个函数,系统就拿operator=(Matrix)代替了)
tmp再次作为实参传入,而你的型参是非引用类型的,因此会再次执行复制构造函数,生成局部变量m,
然后函数结束,C被拷贝,析构m,

最后语句结束,析构tmp

所以在这个语句中,如果你没有复制构造函数,那么同一块内存同时被ma,tmp和m指向,当然会报错。实际上,在析构m的时候就已经报错了,根本轮不到析构tmp

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 17:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表