Thread: Matrix Class Operator Overloading.

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    Matrix Class Operator Overloading.

    Hello, I am making a matrix class with basic operations like addition, subtraction, Multiplication.
    I have made a constructor that takes as input the rows and columns. I am done with
    most of the functions but I am stuck with operator overloading.
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Matrix
    {
     int rows;
     int columns;
     int **matrix;
    public:
     
     Matrix(int r, int c)
     {
    	 rows = r;
         columns = c;
         matrix = new int *[rows];
         for (int i = 0; i < rows; i++)
        	matrix[i] = new int[columns];
     }
    
     int GetRows()
     {
    	 return rows;
     }
    
     int GetColumns()
     {
    	 return columns;
     }
    
     void IdentityMatrix()
     {
    	 if (rows == columns)
    	 {	
    		 for (int i = 0; i < rows; i++)
    			 for (int j = 0; j < columns; j++)
    			 {
    				 if ( i == j )
    				 {
    					 matrix[i][j]= 1;
    				 }
    				 else 
    				 {
    					 matrix[i][j]= 0;
    				 }
    			 }
    	 }
    	 else 
    	 {
    		 cout<<"Rows and Columns not equal, Can make identity Matrix";
    	 }
     }
    
     void ZeroMatrix()
     {
    	 for (int i = 0; i < rows; i++)
    		 for (int j = 0; j < columns; j++)
    			 matrix[i][j] = 0;
     }
    
     void DiagonalMatrix()
     {
    	for (int i = 0; i < rows; i++)
    		 for (int j = 0; j < columns; j++)
    		 {
    			 if ( i == j )
    			 {
    				continue;
    			 }
    			 else 
    			 {
    				 matrix[i][j]= 0;
    			 }
    		 }
     }
    
     void SetValues()
     {
    	 for (int i = 0; i< rows; i++)
    	   for (int j = 0; j< columns ;j++)
    		   cin>>matrix[i][j];
     }
    
     int GetNumber(int i, int j)
     { 
    	 return matrix[i][j];
     }
    
     void SetNumber(int i, int j , int number)
     { 
    	 matrix[i][j] = number;
     }
    
     void ScalarMultiplication(int number)
     {
    	 for (int i = 0; i< rows; i++)
    	 {
    		  for (int j = 0; j< columns ;j++)
    		  {
    			  matrix[i][j] = number*matrix[i][j];
    		  }
    	 }
     }
    
     Matrix operator + (Matrix &a, Matrix &b) I am getting error here, 
    Like I know whenever I create the object, I will have to mention the rows and columns,
     But i cant figure out how here??     
     {
    	 //if rows and coulmns of b are equal then 
    	 //add them and store the result in result matrix
    	 Matrix result;
    	 for (int i = 0; i< rows; i++)
    	 {
    		  for (int j = 0; j< columns ;j++)
    		  {
    			  result[i][j] = a[i][j] + b[i][j];
    		  }
    	 }
    	 return result;
     }
    
      void operator = (Matrix a, Matrix b)
     {
    	 //if a and b are equal 
    	 //then assign b to a
    	
    	 for (int i = 0; i< rows; i++)
    	 {
    		  for (int j = 0; j< columns ;j++)
    		  {
    			  a[i][j] = b[i][j];
    		  }
    	 }
    	
     }
    
    
     void Print()
     {
    	  for (int i = 0; i< rows; i++)
    	  {
    		  for (int j = 0; j<columns ;j++)
    		  {
    			  cout<<matrix[i][j];
    			  cout<<"  ";
    		  }
    	   cout<<endl;
    	  }
     }
    
    };
    int main()
    {
    	Matrix matrix(4,6);
    	matrix.SetValues();
    	Matrix matrix2(4,6);
    	matrix.SetValues();
    
    	Matrix c(4,6);
    	c = matrix + matrix2;
    	c.Print();
    	cout<<endl;
    
    	cout<<matrix.GetNumber(3,3); 
    	cout<<endl;
    
    	matrix.SetNumber(3,3, 5);
    	matrix.Print();
    	cout<<endl;
    
    	matrix.ScalarMultiplication(5);
    	matrix.Print();
    	cout<<endl;
    
    
    	matrix.DiagonalMatrix();
    	matrix.Print();
    	cout<<endl;
    	
    	matrix.IdentityMatrix();
    	matrix.Print();
    	cout<<endl;
    	
    	matrix.ZeroMatrix();
    	matrix.Print();
    	cout<<endl;
    	
    	system("Pause");
    	return 0;
    }
    Last edited by Fatima Rizwan; 09-15-2010 at 06:53 AM.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    If the operator is implemented as a class method, it shall take one parameter instead of two (the first operand is *this). If you implement it outside the class, then you need to supply the first operand either.
    And you should add the 'const' modifier to the parameters:

    Code:
    Matrix operator + (const Matrix& A) {
    }
    Whenever you say 'I'm getting error' please tell what the error message is.

    Code:
     void operator = (Matrix a, Matrix b)
     {
    	 //if a and b are equal 
    	 //then assign b to a
    	
    	 for (int i = 0; i< rows; i++)
    	 {
    		  for (int j = 0; j< columns ;j++)
    		  {
    			  a[i][j] = b[i][j];
    		  }
    	 }
    	
     }
    What does the comment mean? Even if you implement this operator as a global function, it wouldn't work since you use a copy of 'Matrix'. The first operand should be Matrix& and the second one const Matrix&.
    Last edited by kmdv; 09-15-2010 at 08:07 AM.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Matrix operator + (const Matrix &a,const Matrix &b)

    Error:binary 'operator +' has too many parameters.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    I can't help you unless you post the complete code.
    You haven't probably made all the changes:
    If you implement it inside the function you need to supply ONE parameter.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Matrix
    {
     int rows;
     int columns;
     int **matrix;
    public:
    	
    Matrix()
    {
    	int rows = 0;
    	int columns = 0;
    	matrix = new int *[rows];
         for (int i = 0; i < rows; i++)
        	matrix[i] = new int[columns];
    }
     Matrix(int r, int c)
     {
    	 rows = r;
         columns = c;
         matrix = new int *[rows];
         for (int i = 0; i < rows; i++)
        	matrix[i] = new int[columns];
     }
    
     int GetRows()
     {
    	 return rows;
     }
    
     int GetColumns()
     {
    	 return columns;
     }
    
     void IdentityMatrix()
     {
    	 if (rows == columns)
    	 {	
    		 for (int i = 0; i < rows; i++)
    			 for (int j = 0; j < columns; j++)
    			 {
    				 if ( i == j )
    				 {
    					 matrix[i][j]= 1;
    				 }
    				 else 
    				 {
    					 matrix[i][j]= 0;
    				 }
    			 }
    	 }
    	 else 
    	 {
    		 cout<<"Rows and Columns not equal!!";
    	 }
     }
    
     void ZeroMatrix()
     {
    	 for (int i = 0; i < rows; i++)
    		 for (int j = 0; j < columns; j++)
    			 matrix[i][j] = 0;
     }
    
     void DiagonalMatrix()
     {
    	for (int i = 0; i < rows; i++)
    		 for (int j = 0; j < columns; j++)
    		 {
    			 if ( i == j )
    			 {
    				continue;
    			 }
    			 else 
    			 {
    				 matrix[i][j]= 0;
    			 }
    		 }
     }
    
     void SetValues()
     {
    	 for (int i = 0; i< rows; i++)
    	   for (int j = 0; j< columns ;j++)
    		   cin>>matrix[i][j];
     }
    
     int GetNumber(int i, int j)
     { 
    	 return matrix[i][j];
     }
    
     void SetNumber(int i, int j , int number)
     { 
    	 matrix[i][j] = number;
     }
    
     void ScalarMultiplication(int number)
     {
    	 for (int i = 0; i< rows; i++)
    	 {
    		  for (int j = 0; j< columns ;j++)
    		  {
    			  matrix[i][j] = number*matrix[i][j];
    		  }
    	 }
     }
    
     void ScalarAddition(int number)
     {
    	 for (int i = 0; i< rows; i++)
    	 {
    		  for (int j = 0; j< columns ;j++)
    		  {
    			  matrix[i][j] = number + matrix[i][j];
    		  }
    	 }
     }
    
     void ScalarSubtraction(int number)
     {
    	 for (int i = 0; i< rows; i++)
    	 {
    		  for (int j = 0; j< columns ;j++)
    		  {
    			  matrix[i][j] = matrix[i][j] - number;
    		  }
    	 }
     }
    
     void ScalarDivision(int number)
     {
    	 for (int i = 0; i< rows; i++)
    	 {
    		  for (int j = 0; j< columns ;j++)
    		  {
    			  matrix[i][j] = matrix[i][j] / number;
    		  }
    	 }
     }
    
     
     Matrix VectorAdd( Matrix &a,  Matrix &b)
     {
    	 if (a.GetRows()== b.GetRows() && a.GetColumns()==b.GetColumns())
    	 { 
    		 Matrix result(a.GetRows(), a.GetColumns());
    		 for (int i = 0; i< rows; i++)
    		 {
    			  for (int j = 0; j< columns ;j++)
    			  {
    				  result[i][j] = a[i][j] + b[i][j];
    			  }
    		 }
    	 return result;
    	 }
    	 else 
    	 {
    		cout<<"Addition Not possible";
    	 }
    
     }
    
     void operator = (Matrix &a, Matrix &b);
     
     void Print()
     {
    	  for (int i = 0; i< rows; i++)
    	  {
    		  for (int j = 0; j<columns ;j++)
    		  {
    			  cout<<matrix[i][j];
    			  cout<<"  ";
    		  }
    	   cout<<endl;
    	  }
     }
    
    };
    
    void Matrix :: operator = (Matrix &a, Matrix &b)
     {
    	 
    	
    	 for (int i = 0; i< rows; i++)
    	 {
    		  for (int j = 0; j< columns ;j++)
    		  {
    			  a[i][j] = b[i][j];
    		  }
    	 }
    	
     }
    int main()
    {
    	Matrix matrix(4,6);
    	matrix.SetValues();
    	Matrix matrix2(4,6);
    	matrix.SetValues();
    
    	Matrix c(4,6);
    	c.Print();
    	cout<<endl;
    
    	cout<<matrix.GetNumber(3,3); 
    	cout<<endl;
    
    	matrix.SetNumber(3,3,5);
    	matrix.Print();
    	cout<<endl;
    
    	matrix.ScalarMultiplication(5);
    	matrix.Print();
    	cout<<endl;
    
    
    	matrix.DiagonalMatrix();
    	matrix.Print();
    	cout<<endl;
    	
    	matrix.IdentityMatrix();
    	matrix.Print();
    	cout<<endl;
    	
    	matrix.ZeroMatrix();
    	matrix.Print();
    	cout<<endl;
    	
    	system("Pause");
    	return 0;
    }
    
    : error C2804: binary 'operator =' has too many parameters
    : error C2676: binary '[' : 'Matrix' does not define this operator or a conversion to a type acceptable to the predefined operator
    : error C2676: binary '[' : 'Matrix' does not define this operator or a conversion to a type  : error C2676: binary '[' : 'Matrix' does not define this operator or a conversion to a type acceptable to the predefined operator
     : error C2676: binary '[' : 'Matrix' does not define this operator or a conversion to a type acceptable to the predefined operator
     : binary '[' : 'Matrix' does not define this operator or a conversion to a type acceptable to the predefined operator

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733

    Thumbs down

    I meant declaring it as a global function, but stick to the class method idea, that is:

    Code:
    class Matrix {
    
    // ...
    
    Matrix& operator = (const Matrix& A)
    {
        for (int i = 0; i< rows; i++)
        {
            for (int j = 0; j< columns ;j++)
            {
                matrix[i][j] = A.matrix[i][j];
    	}
        }
        return *this;
    }
    
    };
    AND:

    1. supply the destructor to delete[] the matrix!
    2. be careful with the rows and columns, as they might differ in size (like in the operator above).
    3. supply the [] operator:

    Code:
    int* operator [] (int index) {
        return matrix[index];
    }
    const int* operator [] (int index) const {
        return matrix[index];
    }
    The + operator shall have the const modifier too.
    Last edited by kmdv; 09-15-2010 at 08:57 AM.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I meant declaring it as a global function, but stick to the class method idea, that is:
    The compiler would inform you that operator= cannot be overloaded as a global function.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Matrix
    By alex 2010 in forum C++ Programming
    Replies: 0
    Last Post: 06-24-2010, 09:40 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Matrix Class & more...
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2002, 04:59 PM