【问题标题】:no Operator "=" matches these operands , operand types -- Matrix = Matrixno Operator "=" 匹配这些操作数,操作数类型 -- Matrix = Matrix
【发布时间】:2021-11-16 08:03:03
【问题描述】:

我正在编写一个使用运算符重载的矩阵运算程序,但出现“=”运算符重载的错误这是完整的代码。我还通过在“=”运算符声明中使用 (return *this) 返回了我的参考对象,但不知道 为什么它不工作。 当我使用 C = A 时,它没有显示任何错误,但是当我使用 C = A + B 时,就会出现此错误

#include<iostream>
#include<iomanip>
#include<string.h>

using namespace std;

class Matrix{
    private:
        int rows;   //For rows
        int cols;   //For columns
        int **ptr; //pointer to pointer to point in the array 
    public:
    //Declaring Constructors and destructors
        Matrix(Matrix &);
        Matrix(int, int);
        ~Matrix();
    //Declaring other functions
        friend istream& operator>>(istream &, Matrix &);
        friend ostream& operator<<(ostream &,Matrix &);
        Matrix operator +(Matrix &);
        Matrix operator -(Matrix &);
        Matrix operator *(Matrix &);
        Matrix operator =(Matrix &);
        void transpose(Matrix &);
    //End of class definition
};

//Defining Copy constructor
Matrix::Matrix(Matrix &m){
    rows = m.rows;
    cols = m.cols;
    ptr = new int* [rows];
    for(int i= 0;i<rows;i++){
        ptr[i]=new int [cols];
    }
    for(int i =0 ;i<rows;i++){
        for(int j=0;j<cols;j++){
             ptr[i][j] = m.ptr[i][j];
         }
     }
}

//Defining parametrised constructor
Matrix::Matrix(int m, int n){
    rows = m;
    cols = n;
    ptr = new int* [m];
    for(int i =0;i<m;i++){
        ptr[i] = new int[n];
    }
}

//Defining oveloaded operator to take matrix as an input
istream & operator >>(istream &din , Matrix & m){
    for(int i =0;i<m.rows;i++){
        for(int j =0;j<m.cols;j++){
            din >> m.ptr[i][j];
        }
    }
    return din;
}

//Defining overoaded operator to display matrix 
ostream & operator << (ostream &dout,Matrix &m ){
    for(int i =0;i<m.rows;i++){
        for(int j = 0; j<m.cols;j++){
            dout<<setw(5)<< m.ptr[i][j];
        }
        dout<<endl;
    }
    return dout;
}

//Defining overloaded + opearator to add two matrices
Matrix Matrix::operator +(Matrix &m){
    Matrix temp( rows, cols);
    for(int i =0;i<rows;i++){
        for(int j =0;j<cols;j++){
            temp.ptr[i][j]= ptr[i][j] + m.ptr[i][j];
        }
    }
    return temp;
}

//Definig overloaded - operator to subtract two matrices
Matrix Matrix::operator -(Matrix &m){
    Matrix temp(rows,cols);
    for(int i=0;i<rows;i++){
        for(int j=0;j<cols;j++){
            temp.ptr[i][j] = ptr[i][j] - m.ptr[i][j];
        }
    }
    return temp;
}

//Defining overloaded *opeartor to perform matrix multiplication
Matrix Matrix::operator *(Matrix &m){
    Matrix temp(rows,cols);
    int product_sum;
    for(int i =0;i<rows;i++){
        for(int j=0;j<cols;j++){
            product_sum =0;
            for(int k=0;k<rows;k++){
                product_sum += ptr[i][k] * m.ptr[k][j];
            }
            temp.ptr[i][j] = product_sum;
        }
    }
    return temp;
}

//Defining overloaded = operator to assingn matrix value into another matrix  
Matrix Matrix::operator =(Matrix & m){
    for(int i =0;i<rows;i++){
        for(int j=0;j<cols;j++){
            ptr[i][j] = m.ptr[i][j];
        }
    }
    return *this;
}
//Definig a function to perform transpose of a matrix
void Matrix::transpose(Matrix &m){
    for(int i=0;i< rows;i++){
        for(int j=0;j<cols;j++){
            ptr[j][i] = m.ptr[i][j];
        }
    }
}
//Definig Destructor
Matrix::~Matrix(){
    for(int i=0;i<rows;i++){
        delete[] ptr[i];
    }
    delete[] ptr;
}
//Main Function
int main(){
    int row_1 ,row_2,col_1,col_2 ,chs;
    char choice , ch;
    cout<<"\n\n\t\t\t\t\tCreate Matrix A";
    cout<<"\nEnter the number of rows for matrix : ";
    cin>>row_1;
    cout<<"\nEnter the number of columns for matrix : ";
    cin>>col_1;
    Matrix A(row_1,col_1);
    cout<<"\nEnter the elements of Matrix row wise : ";
    cin >> A;
    cout<<"\nEntered Matrix \n";
    cout << A;
    cout<<"\n\n\t\t\tCreate Matrix B";
    cout<<"\nEnter the number of rows for matrix : ";
    cin>>row_2;
    cout<<"\nEnter the number of columns for matrix : ";
    cin>>col_2;
    Matrix B(row_2,col_2);
    cout<<"\nEnter the elements of Matrix row wise : ";
    cin >> B;
    cout<<"\nEntered Matrix\n";
    cout << B;
    Matrix C(col_1,row_1);
    C = A + B; //GETTING ERROR ON THIS LINE
    // **NO Operator "=" matches these operands , operand types -- Matrix = Matrix** 
    return 0;
}

【问题讨论】:

  • 将参数类型从Matrix &amp;更改为const Matrix &amp;
  • 您需要对consts 更加慷慨。
  • @songyuanyao 是啊!我试过了,它在某种程度上开始工作,我正在接受矩阵输入,但是当它添加两个矩阵时,我的程序崩溃显示(分段错误核心转储)我认为我的代码中的内存逻辑存在一些问题

标签: c++ matrix operator-overloading


【解决方案1】:

Matrix Matrix::operator + 返回一个临时值。在 C++ 中,非常量引用不会绑定到临时对象。这是因为非常量引用通常用于修改对象,而临时修改可能会丢失。

但是重载的赋值运算符Matrix Matrix::operator =(Matrix &amp; m)并没有修改m,所以真的可以通过const&取一个临时的:Matrix Matrix::operator =(Matrix const&amp; m)

此外,从技术上讲,返回类型可以是任何东西,甚至是 void,但实际上您应该遵循默认运算符的行为并返回 Matrix&amp; - 对 *this 的引用,而不是副本。

另外,如果您使用std::vector 作为后备存储,则根本不需要这些实用方法!

【讨论】:

  • Matrix Matrix::opeartor +(Matrix &m) 在这种情况下我能做什么,这样我的修改就不会丢失。
  • 我已将返回类型设置为 Matrix& 但问题仍然存在。我想我也必须修改我的其他功能..
  • 非常感谢 :) 现在我明白我的代码有什么问题了
  • @vivekuniyal: operator+ 应该按值返回Matrix - 这是一个新值。返回参考是错误的。现在operator += 应该返回Matrix&amp; - 检查你的教科书是否有区别。
  • 感谢您的指导,现在我明白发生了什么):
【解决方案2】:

问题是这个声明:

Matrix Matrix::operator =(Matrix & m);

你应该把它改成:

Matrix Matrix::operator =(const Matrix & m)

const 引用延长了对象的生命周期;查看更多关于延长对象生命周期的详细信息:Does a const reference class member prolong the life of a temporary?

编辑: 你也应该返回 Matrix&:

Matrix& Matrix::operator =(const Matrix & m)

【讨论】:

  • 我试过了,它在某种程度上开始工作,我正在接受矩阵输入,但是当它添加两个矩阵时,我的程序崩溃显示 (分段错误核心转储) 我认为我的代码中的内存逻辑存在一些问题
  • 返回类型需要改为Matrix&amp;。赋值运算符应该返回一个对被赋值对象的reference。此代码返回的是 copy
  • 在这种情况下,const&amp; 不会延长任何对象的寿命。正如链接问题中的最高答案所述:“只有本地 const 引用才能延长寿命。”这是函数调用中的参数; A+B 临时的生命周期已经是包含该函数调用的整个表达式 C = A+B;
  • 你能解释一下你的答案是如何解决这个问题的吗?看起来你对我的解释的修改建议相同。
  • UPDATE = 解决了这个问题。我了解我的代码中出现问题的地方。问题还在于其他运算符重载。在每种情况下,我都在参数中采用了非常量引用,这导致了数据修改的丢失
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多