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
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;
}```

2. 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&.

3. Matrix operator + (const Matrix &a,const Matrix &b)

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

You haven't probably made all the changes:
If you implement it inside the function you need to supply ONE parameter.

5. 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];
}
}
}

{
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
{
}

}

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. 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.

7. 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.