-1

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;
}
Gomi
  • 3
  • 3
  • You seem to be confused about declaring a new Matrix at the start of your input and output functions. In your input you have a perfectly good Matrix `m1`. It's rows and columns haven't been set to anything, so iterating over them doesn't make sense, but that's the Matrix you should be using. – JohnFilleau Apr 24 '20 at 18:23

1 Answers1

3

Your values are garbage because your constructor doesn't give any values to your variables.

Matrix()
{
    m = new int *[10];
    for (int i = 0; i < 10; i++)
    {
        m[i] = new int[10];
    }
}

Nowhere in this constructor do you give a values to rows, cols, rows1, rows2, cols1 or cols2. So it's not surprising that they have garbage values.

I suppose it should be

Matrix()
{
    m = new int *[10];
    for (int i = 0; i < 10; i++)
    {
        m[i] = new int[10];
    }
    rows = 10;
    cols = 10;
}

Although I don't understand why you have three rows variables and three cols variables.

Another misunderstanding is that you have two variables called rix

int main()
{
    int rosx;
    Matrix m1 = Matrix();
    Matrix m2 = Matrix();
    Matrix rix; // one variable called rix

istream &operator>>(istream &is, Matrix &m1)
{
    //overloaded >> to input the values of a matrix
// PROBLEM IS HERE
    Matrix rix; // another variable called rix

Clearly you think they are the same variable, but they are not. Two variables in different functions are different variables even if they have the same name.

Here's another misunderstanding

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

For some reason you've written this function to print out the variable m but using the rows and columns from a totally different variable called rix. That doesn't make any sense. The rows and columns should come from the variable m, since that is the matrix you are trying to print.

Really I'm just scratching the surface. This code is full of misunderstandings. You need to review how C++ works. In particular how constructors work, how variables work, and just think a little more logically about the code you are writing. It has to make sense.

I would start again. It will be better code second time around.

john
  • 85,011
  • 4
  • 57
  • 81
  • @Gomi, thow it's very nice of you to thank the poster of a good answer to your question, the correct way to show appreciation is to [accept it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if you feel like it solved your problem. – anastaciu Apr 24 '20 at 19:03