Operator Overloaded for Array but not working in Main - c++

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

You've defined the operator + between a MATRIX and a MATRIX[] (i.e., an array of MATRIXs). 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);

Related

Rotate a 2-D array by 90 degrees

How can I take the array size input from the user and pass it to the function. I tried #define inside the function, it doesn't work since the array definition needs the array bound at compile time. I tried global variable too, it says to define a integer constant which is not feasible in my case since I want to get the size from the user. How can I solve this issue?
#include <iostream>
using namespace std;
// reverse the transposed matrix as step 2
void reverseColumns(int arr[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N / 2; j++)
{
int temp = arr[i][j];
arr[i][j] = arr[i][N - j - 1];
arr[i][N - j - 1] = temp;
}
}
}
// take the transpose of matrix as step 1
void transposeMatrix(int arr[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = i; j < N; j++)
{
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
void rotateMatrix(int mat[N][N])
{
transposeMatrix(mat);
reverseColumns(mat);
}
// printing the final result
void displayMatrix(int mat[N][N])
{
int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
cout << mat[i][j] << "\t";
cout << "\n";
}
cout << "\n";
}
int main()
{
int T, N;
cin >> T;
while (T > 0)
{
cin >> N;
int mat[N][N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cin >> mat[i][j];
}
}
int res[N][N];
rotateMatrix(mat);
displayMatrix(mat);
}
return 0;
}
One way to make it work is get the input of rows and cols from user and make a one dimensional array dynamically.
for example:
Let ROWS and COLS be the values you got via cin.
Then the array can be declared as
int* arr = new int[ROWS * COLS];
Instead of writing arr[i][j] you have to write
arr[i * COLS + j]
Also you have to delete the array using
delete[] arr;
You are using C++ so you should take advantages of it. As kiner_shah commented the fast way to fix your code is just by use of std::vector<std::vector<int>>.
This is better solution but stil poor:
#include <iostream>
#include <vector>
using namespace std;
using Matrix = std::vector<std::vector<int>>;
Matrix makeSquereMatrix(size_t N)
{
return {N, std::vector<int>(N)};
}
// reverse the transposed matrix as step 2
void reverseColumns(Matrix& arr)
{
auto N = arr.size();
... // no changes here
}
// take the transpose of matrix as step 1
void transposeMatrix(Matrix& arr)
{
auto N = arr.size();
... // no changes here
}
void rotateMatrix(Matrix& mat)
{
transposeMatrix(mat);
reverseColumns(mat);
}
// printing the final result
void displayMatrix(const Matrix& mat)
{
for (auto& row : mat)
{
for (auto x : row)
cout << x << "\t";
cout << "\n";
}
cout << "\n";
}
void readMatrix(Matrix& m)
{
for (auto& row : m)
{
for (auto& x : row)
{
cin >> x;
}
}
}
int main()
{
int T, N;
cin >> T;
while (T > 0)
{
cin >> N;
auto mat = makeSquereMatrix(N);
readMatrix(mat);
rotateMatrix(mat);
displayMatrix(mat);
--T;
}
return 0;
}
Live demo
Better solution would be introducing a class containing std:::vector with methods performing required actions.
BTW some time ago I've made some matrix code for C. Here is live demo

Matrix C++ problem with overloading operators

I'm making simple matrix calculator for school project. I tried to use overloading operators but I have problem with operator "=". I checked it in debugger in Visual Studio and after assigning data from matrix A to matrix X all of data disappears. How should I repair this? Thank you in advance for your help and advice.
#include <iostream>
#include <vector>
#include <conio.h>
#include <stdlib.h>
using std::vector;
class Matrix {
private:
int rows, cols;
vector<vector<double>> matrix;
public:
Matrix(int r = 0, int c = 0) : rows(r), cols(c) {
std::vector<std::vector<double>> M(rows, std::vector<double>(cols));
matrix = M;
}
~Matrix() {}
// void operator+(const Matrix &rhs);
double get_rows() { return rows; }
double get_cols() { return cols; }
void set_matrix_value() {
std::cout << "Put values in your matrix\n";
for(int i = 0; i < matrix.size(); i++) {
for(int j = 0; j < matrix[i].size(); j++) {
// this->matrix[i][j] = int(_getch() - '0');
std::cin >> matrix[i][j];
}
}
}
void display_matrix() {
for(int i = 0; i < matrix.size(); i++) {
std::cout << "|";
for(int j = 0; j < matrix[i].size(); j++) {
std::cout << matrix[i][j];
if(j != matrix[i].size() - 1) {
std::cout << " ";
}
}
std::cout << "|\n";
}
}
Matrix operator=(const Matrix& M) {
rows = M.rows;
cols = M.cols;
Matrix new_matrix(rows, cols);
std::vector<std::vector<double>> m(rows, std::vector<double>(cols));
new_matrix.matrix = m;
for(int i = 0; i < m.size(); i++) {
for(int j = 0; j < m[i].size(); j++) {
new_matrix.matrix[i][j] = M.matrix[i][j];
}
}
return new_matrix;
}
Matrix operator+(const Matrix& m) {
if(rows != m.rows && cols != m.cols) {
std::cout << "Matrix sizes do not match. Write martix again.";
return (*this);
}
Matrix new_matrix(rows, cols);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
new_matrix.matrix[i][j] = matrix[i][j] + m.matrix[i][j];
}
}
return new_matrix;
}
};
int main() {
// int k;
// int rows, cols;
Matrix X;
// std::cout << "Welcome in Matrix Mode!\n";
X.display_matrix();
Matrix A(3, 3);
A.set_matrix_value();
A.display_matrix();
X = A;
std::cout << X.get_rows() << " " << X.get_cols();
X.display_matrix();
}

Access violation reading location - creating matrix class

This is supposed to be my HW in OOP course. SO i asked to create matrix class. Code works fine - all methods and new operands works fine BUT when my D`tor is empty and when i write there code to free memory i get this error. Thanks for help
int main()
{
MyMatrix m1(3, 3);
MyMatrix m2(3, 3);
MyMatrix res(3, 3);
m1.Set();
m2.Set();
cout << m1;
cout << m2;
res = m1 + m2;
cout << res;
}
CPP
MyMatrix::MyMatrix(int row, int col) // C`tor with specific data
{
n = row;
m = col;
matrix = new int* [n]; // Memory allocation for rows
for (int i = 0; i < n; i++)
{
matrix[i] = new int[m]; // Memory Allocation for columns
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
matrix[i][j] = 0;
}
}
}
MyMatrix::~MyMatrix() // Default d`tor
{
for (int i = 0; i < n; i++)
{
delete[] matrix[i];
}
delete[] matrix;
}
void MyMatrix::Set()
{
cout << "Enter new row" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> matrix[i][j];
}
if (i != (n - 1))
{
cout << "Enter new row" << endl;
}
}
}
ostream& operator<<(ostream& out, const MyMatrix& matrix)
{
for (int i = 0; i < matrix.n; i++)
{
//run in loop on every column.
for (int j = 0; j < matrix.m; j++)
//print value with a space.
out << matrix.matrix[i][j] << "t";
//at the end of every row jump line.
out << endl;
}
out << endl;
return out;
}
MyMatrix& MyMatrix::operator= (const MyMatrix& mat1)
{
n = mat1.n;
m = mat1.m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
matrix[i][j] = mat1.matrix[i][j];
}
}
return *this;
}
const MyMatrix MyMatrix::operator+(const MyMatrix& mat1) const
{
MyMatrix temp(n, m);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
temp.matrix[i][j] = matrix[i][j] + mat1.matrix[i][j];
}
}
return temp;
}
H file
class MyMatrix
{
private:
int **matrix;
int n, m;
public:
MyMatrix(int a, int b);
~MyMatrix();
void Set();
const MyMatrix operator+ (const MyMatrix& mat1) const;
MyMatrix& operator= (const MyMatrix& mat1);
friend ostream& operator<<(ostream& out, const MyMatrix& matrix);
};

How to use function with parameters in class , c++?

I have to sum of two matrices (tridiagonal), everything works, but I don't know how to call function sum in main. I tried but it reports an error. Any help or recommendations would be appreciated.
.h:
class matrix
{
public:
matrix();
~matrix();
matrix(int i, int j);
void insert();
void iz();
void sum(matrix m1, matrix m2);
private:
int i;
int j;
vector<vector<int> > v;
};
.cpp
matrix::matrix()
{
}
matrix::matrix()
{
}
matrix::matrix(int i, int j){
v.resize(i);
for (int k = 0; k < i; k++)
v[k].resize(j);
this->i = i;
this->j = j;
}
void matrix::insert()
{
int x;
for (int a = 0; a < i; a++){
for (int b = 0; b < j; b++){
if (abs(a-b) <= 1){
x = rand() % 100+1;
v[a][b] = x;
}
else
v[a][b] = 0;
}
}
}
void matrix::iz()
{
for (int a = 0; a < i; a++){
for (int b = 0; b < j; b++)
cout << v[a][b] << " ";
cout << endl;
}
}
void matrix::sum(matrix m1, matrix m2)
{
matrix m3;
int c = m1.i;
int d = m1.j;
if (m1.i == m2.i && m1.j == m2.j)
{
for (int a = 0; a < c; a++)
{
for (int b = 0; b < d; b++){
m3.v[a][b] = m1.v[a][b] + m2.v[a][b];
}
cout << endl;
}
}
else
cout << "Error" << endl;
}
main
matrix m1(6, 6);
m1.insert();
m1.iz();
cout << endl << endl;
matrix m2(6, 6);
m2.insert();
m2.iz();
Here is the problem:
/*
matrix m3;
m3.sum(m1, m2);
m3.iz();
*/
cin.ignore();
cin.get();
return 0;
The problem lies in the sum function where you create a new object matrix m3, and perfor the addition on that object, instead of this.
Do: this->v[a][b] = m1.v[a][b] + m2.v[a][b];
instead of: m3.v[a][b] = m1.v[a][b] + m2.v[a][b];
Also, you need to set the this->i and this->j variables to m1.i and m1.j, plus you should also do all your resizing of this->v too.

Class matrix addition

I have a program that is suppose to add together two matrices but when it gets to the add part in the main it just gets stuck and does nothing. I have messed with this for quite some time but to no avail. Any help or recommendations would be appreciated.
#include <iostream>
using namespace std;
class matrix
{
private:
int **p, m, n;
public:
matrix(int row, int col)
{
m = row;
n = col;
p = new int*[m];
for (int i = 0; i < m; i++)
p[i] = new int[n];
}
~matrix()
{
for (int i = 0; i < m; i++)
delete p[i];
delete p;
}
void fill()
{
cout<<"Enter the matrix elements:";
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cin >> p[i][j];
}
}
}
void display()
{
cout <<"The matrix is:";
for(int i = 0; i < m; i++)
{
cout << endl;
for(int j = 0; j < n; j++)
{
cout << p[i][j] <<" ";
}
}
cout << endl;
}
matrix operator +(matrix m2)
{
matrix T(m, n);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
T.p[i][j] = p[i][j] + m2.p[i][j];
}
}
return T;
}
matrix operator =(matrix eq)
{
m = eq.m;
n = eq.n;
p = eq.p;
return *this;
}
friend matrix operator *(matrix, matrix);
};
matrix operator *(matrix a , matrix b)
{
matrix B(1,1);
if(a.n == b.m)
{
matrix T(a.m, b.n);
for(int i = 0; i < a.m; i++)
{
for(int k = 0; k < b.n; k++)
{
T.p[i][k] = 0;
for(int j = 0; j < a.n; j++)
{
T.p[i][k]+= a.p[i][j] * b.p[j][k];
}
}
}
B = T;
}
return B;
}
int main()
{
matrix a(3,3), b(3,3);
a.fill();
a.display();
b.fill();
b.display();
cout << "addition of a and b\n";
b = b + a;
b.display();
cout << "multiplication of a and b\n";
b = (a * b);
b.display();
}
Your program is violating the rule of big three: it has a destructor but no assignment operator and no copy constructor. It keeps the data using raw pointers, but it's not managing proper ownership with copies are done and assignments are performed.
When your matrix class is copied and assigned your program is entering the Undefined Behavior territory so anything can happen. In this code copy construction is done implicitly when passing a matrix parameter by value, and assignment is done explicitly in main.