I wrote a program to add, subtract and multiply two matrices together. I overloaded the operators , -, and * for this purpose, but when I use them in the main, I get an error which says:
no operator " " matches these operands
I can't figure out the problem. Maybe I used incorrect logic to overload the operators for class and now I'm stuck with it. The Code is below
#include <iostream>
#include <conio.h>
#include <string>
#include <ctime>
using namespace std;
class ERROR
{
public:
string E = "Number of Columns of Matrix A is not Equal to Number of Rows of Matrix B";
};
class MATRIX
{
private:
static const int number = 100;
int matrix[number][number];
int n;
int m;
public:
MATRIX();
MATRIX(int,int);
void display();
MATRIX operator (MATRIX x[]);
MATRIX operator-(MATRIX x[]);
MATRIX operator*(MATRIX x[]);
MATRIX operator=(MATRIX x[]);
};
int main()
{
srand(time(0));
MATRIX x(2, 2), y(2, 2);
cout << "Matrix 1 " << endl;
x.display();
cout << endl;
cout << "Matrix 2 " << endl;
y.display();
cout << endl;
MATRIX z(2, 2);
z = x y;
}
// Constructors of Class Matrix
MATRIX::MATRIX()
{
n = number; m = number;
for (int loop = 0; loop < number; loop )
{
for (int loop2 = 0; loop2 < number; loop2 )
{
matrix[loop][loop2] = 1 rand() % 50;
}
}
}
MATRIX::MATRIX(int rows, int col)
{
n = rows; m = col;
for (int loop = 0; loop < n; loop )
{
for (int loop2 = 0; loop2 < m; loop2 )
{
matrix[loop][loop2] = 1 rand() % 50;
}
}
}
// Display function for Class
void MATRIX::display()
{
for (int loop = 0; loop < n; loop )
{
for (int loop2 = 0; loop2 < m; loop2 )
{
cout << matrix[loop][loop2] << " ";
}
cout << endl;
}
cout << endl;
}
// Operators Overloaded for Array
MATRIX MATRIX::operator (MATRIX x[])
{
MATRIX z(n,m);
for (int loop = 0; loop < n; loop )
{
for (int loop2 = 0; loop2 < m; loop2 )
{
z.matrix[loop][loop2]= matrix[loop][loop2] x->matrix[loop][loop2];
}
}
return z;
}
MATRIX MATRIX::operator-(MATRIX* x)
{
MATRIX z(n, m);
for (int loop = 0; loop < n; loop )
{
for (int loop2 = 0; loop2 < m; loop2 )
{
z.matrix[loop][loop2] = matrix[loop][loop2] - x->matrix[loop][loop2];
}
}
return z;
}
MATRIX MATRIX::operator*(MATRIX x[])
{
try
{
if (m == x->n)
{
MATRIX *y[number][number];
for (int loop = 0; loop < n; loop )
{
for (int loop2 = 0; loop2 < m; loop2 )
{
for (int loop3 = 0; loop3 < m; loop3 )
{
y[loop][loop2] = matrix[loop][loop3] * x->matrix[loop3][loop2];
}
}
}
return *this;
}
throw;
}
catch (ERROR e)
{
cout << e.E << endl;
_getch();
exit(1);
}
}
MATRIX MATRIX::operator=(MATRIX x[])
{
for (int loop = 0; loop < n; loop )
{
for (int loop2 = 0; loop2 < m; loop2 )
{
matrix[loop][loop2] = x->matrix[loop][loop2];
}
}
return *this;
}
CodePudding user response:
You've defined the operator
between a MATRIX
and a MATRIX[]
(i.e., an array of MATRIX
s). You should amend the definition to operate on a MATRIX
and another MATRIX
:
MATRIX operator (MATRIX x);
and of course, amend the implementation accordingly.
EDIT:
As Fabien mentioned in the comments, using a const reference will save copying the second operand when using this operator:
MATRIX operator (const MATRIX& x);