0

I am trying to write a cpp program to do matrix operations with operator overloading.

My class Matrix has the following variables:

int m,n // order of the matrix
int **M;

At first, I had a constructor and a destructor using new and delete operators to allocate and deallocate memory for **M. I also had functions to overload +,- and * operators. But when I run the program, I got garbage values as results. Also, during runtime, I got an error (glibc detected).

Similar questions here told me that I should add a copy constructor that "deep-copies" the 2D array. I did this too. But the same problem persisted.

So I added a function to overload = operator. Now, I am getting compile time error (no matching function for call to ‘Matrix::Matrix(Matrix)’) whenever I use the '=' operator.

Here are my functions:

copy constructor

Matrix(Matrix& other)  {
  m=other.m;
  n=other.n;

  M= new int *[m];
  for(int i=0;i<m;i++)
    M[i] = new int[n];

  //deep copying matrix
  for(int i=0;i<m;i++)
    for(int j=0;j<n;j++)
      M[i][j]=other.M[i][j];
}

overloading * :

Matrix Matrix::operator*(Matrix A)  {
  Matrix pro(m,A.n);
  for(int i=0;i<m;i++)
    for(int j=0;j<A.n;j++) {
      pro.M[i][j]=0;
      for(int k=0;k<n;k++)
        pro.M[i][j]+=(M[i][k]*A.M[k][j]);
    }
  return pro;
}

overloading = :

Matrix Matrix::operator=(Matrix a)  {
  m=a.m;
  n=a.n;
/* 
  M=new int* [m];
  for(int i=0;i<m;i++) //I also tried allocating memory in this function
    M[i]=new int[n];
*/
  for(int i=0;i<m;i++)
    for(int j=0;j<n;j++)
      M[i][j]=a.M[i][j];
  return *this;
}

in main() :

Matrix M1(m,n);
Matrix M2(p,q);

//inputting both matrices

Matrix M3(m,n); 
Matrix M4(m,q);

M3 = M1 + M2;  // Compile Time Error here...
M3.show();

M3 = M1 - M2;  //...here...
M3.show();

M4 = M1*M2;   //...and here.
M4.show();

Compile Time Error: no matching function for call to ‘Matrix::Matrix(Matrix)’

user3125280
  • 2,779
  • 1
  • 14
  • 23
arry36
  • 142
  • 10
  • `Matrix(Matrix&)` I strongly doubt you want that. Maybe with `const`? – Marc Glisse Jan 19 '14 at 10:36
  • You should also use `const&` for the arguments in the other functions, to avoid constantly copying matrices. – Marc Glisse Jan 19 '14 at 10:38
  • `Matrix Matrix::operator=(Matrix a)... return *this;` isn't that wrong? It would require a Matrix(Matrix) constructor – user3125280 Jan 19 '14 at 11:39
  • If the purpose of this exercise is to learn how to write a matrix class and overload operators properly, just throw away the manual memory management and use a `std::vector>` which does the job for you. On the other hand, if the purpose of the exercise is to learn how to manage resources in C++, read about the Rule Of Three (The Rule of Five in C++11), RAII, and finaly The Rule Of Zero (In that order please). – Manu343726 Jan 19 '14 at 11:41
  • For operator overloading, the wiki has a very good thread about the topic. Also the rule of three/five is a topic covered in many questions, so this is definitively a duplicate. – Manu343726 Jan 19 '14 at 11:46
  • @Manu343726 It is a duplicate if the question is the same, not the answer. Question is "where does Compile Time Error: no matching function for call to ‘Matrix::Matrix(Matrix)’" come from?" – user3125280 Jan 19 '14 at 11:47
  • possible duplicate of [Operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading) – Manu343726 Jan 19 '14 at 11:48

2 Answers2

1
Matrix& Matrix::operator=(const Matrix& a)  {
  m=a.m;
  n=a.n;
/* 
  M=new int* [m];
  for(int i=0;i<m;i++) //I also tried allocating memory in this function
    M[i]=new int[n];
*/
  for(int i=0;i<m;i++)
    for(int j=0;j<n;j++)
      M[i][j]=a.M[i][j];
  return *this;
}

The assignment operator has the wrong signature, so that return *this is trying to call a constructor of type Matrix(Matrix), which doesn't exist. Make sure to return a reference like above.

user3125280
  • 2,779
  • 1
  • 14
  • 23
0

Apart from the other answers speaking about effective implementation of copy-constructor and assignment-operator (your code isn't very effective, but it should work), there seems to be only a little mistake:

Matrix(Matrix& other) { ... } seems to be out of namespace. Change it to:

Matrix::Matrix(const Matrix& other) { ... }

mb84
  • 683
  • 1
  • 4
  • 13