当前位置: 首页 > 知识库问答 >
问题:

重载operator=不与赋值一起编译

马胜泫
2023-03-14

github repo用代码尝试编写带有重载某些操作的矩阵类。
当我尝试用此笔画编译时,一切都出错了

result = (l_mtx + r_mtx);

Matrix.cpp:在函数“int main()”中:
Matrix.cpp:36:12:错误:对“Matrix::Matrix(Matrix)”的调用没有匹配函数

result = (l_mtx + r_mtx);   

然后是这个函数的几个候选项,我真的不明白。
我认为有copy构造函数和几个构造函数,但这不是operator=我认为应该在笔画中调用的。

matrix_class.h:73:5:注意:matrix::matrix(Matrix&)[类型=double]
(参数1没有已知的从“matrix”到“matrix&"的转换)

matrix_class.h:46:5:注意:matrix::matrix(int,int)[with type=double]
(候选者需要2个参数,提供1个参数)

matrix_class.h:39:5:注意:matrix::matrix()[使用类型=double]
(候选项需要0个参数,提供1个参数)

然后出现错误:
matrix_class.h:96:18:错误:初始化“Matrix Matrix::operator=(Matrix)[with type=double]”的参数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);
    }
}

共有1个答案

李浩邈
2023-03-14

声明复制赋值运算符,如

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);
    }
}
 类似资料:
  • 您可以重新定义或重载C#中可用的大多数内置运算符。 因此,程序员也可以使用具有用户定义类型的运算符。 重载运算符是具有特殊名称的函数,关键字operator后跟要定义的运算符的符号。 与任何其他函数类似,重载运算符具有返回类型和参数列表。 例如,通过以下功能 - public static Box operator+ (Box b, Box c) { Box box = new Box();

  • 您可以重新定义或重载F#中可用的大多数内置运算符。 因此,程序员也可以使用具有用户定义类型的运算符。 运算符是具有特殊名称的函数,括在括号中。 必须将它们定义为静态类成员。 与任何其他函数一样,重载运算符具有返回类型和参数列表。 以下示例显示了复数上的+运算符 - //overloading + operator static member (+) (a : Complex, b: Complex

  • 下标operator []通常用于访问数组元素。 可以重载此运算符以增强C ++数组的现有功能。 下面的示例解释了下标operator []如何重载。 #include <iostream> using namespace std; const int SIZE = 10; class safearay { private: int arr[SIZE]; public:

  • 函数调用operator()可以为类类型的对象重载。 当您重载()时,您没有创建一种调用函数的新方法。 相反,您正在创建一个可以传递任意数量参数的运算符函数。 下面的示例解释了函数调用operator()如何重载。 #include <iostream> using namespace std; class Distance { private: int feet;

  • 在《 到底什么时候会调用拷贝构造函数?》一节中,我们讲解了初始化和赋值的区别:在定义的同时进行赋值叫做 初始化(Initialization),定义完成以后再赋值(不管在定义的时候有没有赋值)就叫做 赋值(Assignment)。初始化只能有一次,赋值可以有多次。 当以拷贝的方式初始化一个对象时,会调用拷贝构造函数;当给一个对象赋值时,会调用重载过的赋值运算符。 即使我们没有显式的重载赋值运算符,

  • C++ 重载运算符和重载函数 就像其他运算符一样,您可以重载赋值运算符( = ),用于创建一个对象,比如拷贝构造函数。 下面的实例演示了如何重载赋值运算符。#include <iostream> using namespace std; class Distance { private: int feet; // 0 到无穷 int inches; // 0 到 12 public: // 所需的