0

I am trying to overload the operator << for cout for a dynamic array in a class. My class and member functions are as follows:

class Matrix{
private:
    int rows;
    int columns;
    double* matrix;
public:
    Matrix();
    explicit Matrix(int N);
    Matrix(int M, int N);
    void setValue(int M, int N, double value);
    double getValue(int M, int N);
    bool isValid() const;
    int getRows();
    int getColumns();
    ~Matrix();
    friend ostream& operator<<(ostream &out, const Matrix&matrix1);

};

    Matrix::Matrix(){
    matrix = NULL;
}

Matrix::Matrix(int N){
    matrix = new double[N * N];
    rows = N;
    columns = N;

    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            if(i==j)
                matrix[i * N + j] = 1;
            else
                matrix[i * N + j] = 0;
        }
    }
}

Matrix::Matrix(int M, int N){
    matrix = new double[M * N];
    rows = M;
    columns = N;

    for(int i = 0; i < M; i++){
        for(int j = 0; j < N; j++)
            matrix[i * N + j] =  0;
    }
}

Matrix::~Matrix(){
    delete [] matrix;
}

void Matrix::setValue(int M, int N, double value){
    matrix[M * columns + N] = value;
}

double Matrix::getValue(int M, int N){
    return matrix[M * columns + N];
}

bool Matrix::isValid() const{
    if(matrix==NULL)
        return false;
    else
        return true;
}

int Matrix::getRows(){
    return rows;
}

int Matrix::getColumns(){
    return columns;
}

I've tried to implement the << operator as follows:

ostream& operator<<(ostream &out, const Matrix&matrix1){
Matrix mat1;
int C = mat1.getColumns();
int R = mat1.getRows();

for(int i = 0; i < R; i++){
    for(int j = 0; j < C; j++)
        out << mat1.getValue(i,j) << "\t";
    out << endl;
}
return out;

}

and call it from a function:

void test(){
Matrix mat1(3,4);
cout << mat1 << endl;

}

but this doesn't print anything at all. It seems like the overload functions doesn't get any values for C and R, but I might be wrong. Anyone got some ideas?

It is suppose to print dynamic matrices on the form

a11     a12     a13    . . .
a21     a22     a23    . . .
.        .       .     . . .
.        .       .     . . .
.        .       .     . . .
3
  • "It seems like the overload functions doesnt get any values for C and R, but I might be wrong" - Did you set a breakpoint and check the values of C and R? Commented Mar 12, 2013 at 21:37
  • You're still creating unnecessary local variables, just like you were doing with the constructors earlier. Commented Mar 12, 2013 at 21:41
  • No, I didn't but thanks for the tip! Commented Mar 12, 2013 at 21:43

3 Answers 3

2

You are printing an empty mat1, not given matrix1.

ostream& operator<<(ostream &out, const Matrix& matrix1)
{
    //Matrix mat1;   // <- Comment ythis
    int C = matrix1.getColumns(); // <<- matrix1
    int R = matrix1.getRows(); // <<- matrix1

    for (int i = 0; i < R; i++)
    {
        for (int j = 0; j < C; j++)
            out << matrix1.getValue(i, j) << "\t"; // <<- matrix1
        out << endl;
    }
    return out;
}
Sign up to request clarification or add additional context in comments.

Comments

2

You need to print the contents of matrix1. You are creating a local empty matrix mat1 and printing its contents instead.

ostream& operator<<(ostream &out, const Matrix& matrix1) 
{
  int C = matrix1.getColumns();
  int R = matrix1.getRows();

  for(int i = 0; i < R; i++){
    for(int j = 0; j < C; j++)
        out << matrix1.getValue(i,j) << "\t";
    out << endl;
  }
  return out;

}

Comments

2

Am I seeing this right, Matrix1 is the parameter to operator<< but you are creating a new mat1 and outputting that?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.