I can't seem to access class variables through functions or otherwise, side of my overloaded << friend function. Values appear to be random numbers inside of the functions even though I redefine them in the main.
I am in the process of making a program that does various matrix functions using overloaded operators. Right now I am trying to make the functions work with matrices of sizes that aren't 3x3 and these worked earlier with the global variable s
, but I would like rows
to be redefined before <<
and >>
are called.
#include <iostream>
using namespace std;
#define s 3
class Matrix
{
private:
public:
int rows,cols;
int rows1, rows2, cols1, cols2;
int **m;
void setRows(int rowsa);
int getRows();
Matrix()
{
m = new int *[10];
for (int i = 0; i < 10; i++)
{
m[i] = new int[10];
}
}
friend Matrix operator+(Matrix &m1, Matrix &m2);
friend Matrix operator-(Matrix &m1, Matrix &m2);
friend Matrix operator*(Matrix &m1, Matrix &m2);
friend ostream &operator<<(ostream &os, Matrix &m1);
friend istream &operator>>(istream &is, Matrix &m1);
};
Matrix operator+(Matrix &m1, Matrix &m2)
{
Matrix m3 = Matrix();
for (int i = 0; i < s; i++)
{
for (int j = 0; j < s; j++)
m3.m[i][j] = m1.m[i][j] + m2.m[i][j];
}
return m3;
}
Matrix operator-(Matrix &m1, Matrix &m2)
{
Matrix m3 = Matrix();
for (int i = 0; i < s; i++)
{
for (int j = 0; j < s; j++)
m3.m[i][j] = m1.m[i][j] - m2.m[i][j];
}
return m3;
}
Matrix operator*(Matrix &m1, Matrix &m2)
{
Matrix m3 = Matrix();
Matrix rix;
for (int i = 0; i < rix.rows1; i++)
{
for (int j = 0; j < rix.cols2; j++)
{
for (int k = 0; k < rix.cols1; k++)
{
m3.m[i][j] = m1.m[i][k] * m2.m[k][j];
}
}
}
return m3;
}
ostream &operator<<(ostream &os, Matrix &m)
{
Matrix rix;
for (int i = 0; i < rix.rows; i++)
{
for (int j = 0; j < rix.cols; j++)
os << m.m[i][j] << " ";
os << "\n";
}
return os;
}
istream &operator>>(istream &is, Matrix &m1)
{
//overloaded >> to input the values of a matrix
// PROBLEM IS HERE
Matrix rix;
cout<<"value of rows1"<<rix.rows1<<endl;
cout<<"value of rows"<<rix.rows<<endl;
cout<<"value of getRows"<<rix.getRows()<<endl;
int k;
for (int i = 0; i < rix.rows; i++)
{
for (int j = 0; j < rix.cols; j++)
{
cout << "Enter element "
<< "(" << i << "," << j << "): ";
cin >> k;
m1.m[i][j] = k;
}
}
}
void Matrix::setRows(int rowsa){
rows= rowsa;
}
int Matrix::getRows(){
return rows;
}
int main()
{
int rosx;
Matrix m1 = Matrix();
Matrix m2 = Matrix();
Matrix rix;
cout << "Number of rows for the first Matrix? (max 10)" << endl;
cin >> rix.rows1;
cout << "Number of columns for the first Matrix? (max 10)" << endl;
cin >> rix.cols1;
cout << "Number of rows for the second Matrix? (max 10)" << endl;
cin >> rix.rows2;
cout << "Number of columns for the second Matrix? (max 10)" << endl;
cin >> rix.cols2;
cout << "input the elements of the first matrix: " << endl;
rix.setRows(rix.rows1);
cout <<rix.rows;
cin >> m1;
cout << m1;
cout << "input the elements of the second matrix: " << endl;
rix.rows = rix.rows2;
rix.cols = rix.cols2;
cin >> m2;
cout << m2;
Matrix m3 = m1 * m2;
cout << "output" << endl;
cout << m3;
return 0;
}