I am writing a program for Matrix operation using operator overloading but getting an error for "=" operator overloading Here's the full code. I have also returned my reference object by using (return *this) in " = " operator declaration but don't know why it is not Working . When i am using C = A it's not showing any errors but when i am using C = A B then this error is coming up
#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;
}
CodePudding user response:
Matrix Matrix::operator
returns a temporary. In C , non-const references will not bind to temporaries. This is because a non-const reference generally is used to modify an object, and with a temporary such a modification would likely be lost.
But the overloaded assignment operator Matrix Matrix::operator =(Matrix & m)
does not modify m
, so it can really take a temporary by const&: Matrix Matrix::operator =(Matrix const& m)
.
Furthermore, the return type technically can be anything, even void
, but in practice you should follow the behavior of the default operator and return Matrix&
- a reference to *this
, not a copy.
Also, if you used a std::vector
as the backing store, you wouldn't need most of these utility methods at all!
CodePudding user response:
The problem is this declaration:
Matrix Matrix::operator =(Matrix & m);
You should change it to:
Matrix Matrix::operator =(const Matrix & m)
The const
reference prolongs the object's life; check this out for more details about extending an object's lifetime: Does a const reference class member prolong the life of a temporary?
EDIT: yous should also return Matrix&:
Matrix& Matrix::operator =(const Matrix & m)