重载运算符 = 不使用赋值进行编译

Overloading operator= doesn't compile with assigning

本文关键字:赋值 编译 运算符 重载      更新时间:2023-10-16

github存储库与代码尝试使用重载某些操作来编写矩阵类。
当我尝试用这个笔画编译时,一切都出错

result = (l_mtx + r_mtx);

我从 g++ 收到错误:
g++ -g3 -std=c++11 -wall -o 矩阵 matrix_class.h 矩阵.cpp

matrix.cpp:在函数 'int main((' 中:
矩阵.cpp:36:12:错误:调用"矩阵::矩阵(矩阵("没有匹配函数

result = (l_mtx + r_mtx);   

然后是这个函数的几个候选者,我真的不明白。
我认为有复制构造函数和几个构造函数,但这不是我认为应该在该笔画中计算的运算符=。

matrix_class.h:73:5:注意:矩阵::矩阵(矩阵&( [类型=双精度]
(参数 1 没有从"矩阵"到"矩阵"的已知转换)

matrix_class.h:46:5:注意:矩阵::矩阵(整数,整数([类型=双精度]
(考生期望 2 个参数,提供 1 个(

matrix_class.h:39:5:注意:矩阵::矩阵(( [类型 = 双精度]
(候选人期望 0 个参数,提供 1 个(

然后是错误:
matrix_class.h:96:18:错误:初始化"矩阵矩阵::运算符=(矩阵([类型=双精度]"的参数

1

我认为我没有正确编码分配运算符或复制构造函数,但我找不到错误在哪里。对不起,愚蠢的问题。感谢您的关注。

//copy constructor
    Matrix(const Matrix<type> &org)
    {
        cout << "Making a copy of " << this << endl;
        row = org.getRow();
        column = org.getColumn();
        //allocate additional space for a copy
        data = new type* [row];
        for (int i = 0; i < row; ++i)
        {
            data[i] = new type [column];
        }
        for (int i = 0; i < row; ++i)
        {
            for (int j = 0; j < column; ++j)
            {
                data[i][j] = org.data[i][j];
            }
        }
    }

和运算符=

//assign constructor
Matrix<type> operator = (Matrix<type> r_mtx)
{
    if (row == r_mtx.getRow())
    {
        if (column == r_mtx.getColumn())
        {
            //TODO: удалить прежний объект?
            Matrix<type> temp(row, column);
            for (int i = 0; i < row; ++i)
            {
                for (int j = 0; j < column; ++j)
                {
                    temp.data[i][j] = r_mtx[i][j];
                }
            }
            return temp;
        }
        else
        {
            cout << "Assign error: matrix column are not equal!" << endl;
            exit(EXIT_FAILURE);
        }
    }
    else
    {
        cout << "Assign error: matrix rows are not equal!" << endl;
        exit(EXIT_FAILURE);
    }
}

声明复制赋值运算符,如下所示

Matrix<type> & operator = ( const Matrix<type> &r_mtx )

问题是临时对象可能未绑定到非常量引用。

考虑到赋值运算符应返回对左侧对象的引用。

您的赋值运算符实质上是无效的。它不是分配左侧对象,而是创建一个临时对象。所以没有任何分配。

它可以被定义为这样

Matrix<type> & operator = ( const Matrix<type> &r_mtx )
{
    if (row == r_mtx.getRow())
    {
        if (column == r_mtx.getColumn())
        {
            for (int i = 0; i < row; ++i)
            {
                for (int j = 0; j < column; ++j)
                {
                    data[i][j] = r_mtx[i][j];
                }
            }
            return *this;
        }
        else
        {
            cout << "Assign error: matrix column are not equal!" << endl;
            exit(EXIT_FAILURE);
        }
    }
    else
    {
        cout << "Assign error: matrix rows are not equal!" << endl;
        exit(EXIT_FAILURE);
    }
}

这是一个更简单的建议,使用复制和交换习惯用法的变体:

Matrix<type> & operator = ( Matrix<type> r_mtx )  //  pass by value
{
    if ( getRow() != r_mtx.getRow() || getColumn() != r_mtx.getColumn() )
        throw std::runtime_error("Wrong dimension in matrix assigment");
    std::swap(data, r_mtx.data);
    return *this;
}

如果要允许分配,即使目标矩阵的大小已经不正确,则可以取出维度检查,并复制或交换rowcolumn(以及任何其他成员变量(。

使用 throwexit更可取,因为它为代码的用户提供了更多选项。如果他们不catch,那么无论如何都相当于退出。