Hi, i've trying to make a program that reads data for a matrix and print the data for it. I can get the ostream operator to work, however the istream operator wont work. Any got some ideas to help me sort out the istream operator:

This is my header file:

#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
using namespace std;

class Matrix{
	
private:
	int ** value;
	int row;
	int colum;

public:
	Matrix();	
	Matrix(int ro, int col);
	Matrix(const Matrix& org);

	//Operator
	friend istream& operator>>(istream& in, Matrix& ho);
	friend ostream& operator<<(ostream& out, const Matrix& ho);
};

#endif

This is my .cpp file:

#include "Matrix.h"
#include <iostream>
using namespace std;



Matrix::Matrix(int ro, int col){
	
	row = ro;
	colum = col;
	value = new int*[row];

	for (int i = 0; i < row; i++){
	value[i] = new int[colum];
	}
}

Matrix::Matrix(const Matrix& org)

	value = new int*[row];
	for (int i = 0; i < row; i++){
	vaule[i] = org.value[i];
		for(int j = 0; j < colum;i++){
		value[j] = org.value[j];
		}
	}
}

//Here are where the problems starts.
istream& operator>>(istream& in, Matrix& ho){
	

	int line; // I'm not sure what the input should be when i'm trying to put it in a value pointer that points to a  
                    // table of data.
	in >> line;

	for(int i = 0; i < ho.row; i++){
		for (int j = 0; j < ho.colum; j++){
			ho.value[j] = line[j];
		}
	}
	return in;

}	


ostream& operator<<(ostream& out, const Matrix& ho){
		
	ut << '[';
	for (int i=0;i < ho.row; i++){			
		for (int j = 0; j < ho.colum; j++){
			if(i == 0 && j == 0){
				ut << ho.value[0];
			}
			else{
			ut << ',' << ho.value[j];
			}
                }

	}
	ut << ']';
	return ut;
}

So the problem is that i cant get the input to work. When i compile it there is allways different errors. what ever i try. Maybe i think the wrong way. But the main thing is that i have a double pointer. That points to a table which is the colums which then points to a new table that have the rows. Thats how the assignment is.

Thanks in advance.

Regards Barvik

Recommended Answers

All 2 Replies

The idea is to read what the << operator writes. In this case, your << operator writes a comma separated list of the values, all wrapped in brackets. So if you have the following matrix:

{0, 1, 2, 3},
{4, 5, 6, 7}

The output would be "[0,1,2,3,4,5,6,7]". Your >> operator needs to parse this string and extract the values:

istream& operator>> ( istream& in, Matrix& m )
{
  in.ignore(); // Skip the opening bracket

  for ( int i = 0; i < row; i++ ) {
    for ( int j = 0; j < column; j++ ) {
      int x;

      in>> x;
      value[i][j] = x;
      in.ignore(); // Skip the comma
    }
  }

  in.ignore(); // Skip the closing bracket

  return in;
}

Of course, that's only a part of your problems. You don't seem to know how to access Matrix::value properly, and there are syntax errors in the code you've posted. But that's a different question, so I'll leave you to work it out on your own until you need more help.

Heaps of things wrong with your code.

1) You need to decide if you are working with a 1D or 2D data structure.

2) You need to decide whether your copy constructor does a deep copy (i.e. dynamically allocating rows and columns, and then copying all the values). At the moment you're doing (sort-of) a shallow copy - which means that a copy of a matrix shares memory (and therefore data) with the original. That means changing one Matrix will change the other.

3) your operator<< outputs values of pointers (value[j] is of type pointer to int). It is not outputting the actual data stored in the matrix. The only way reading in will work again is if your program does not change those pointers. You are essentially relying on your data to be hard-coded into your program if you read in pointer values.

4) As a general rule, the logic of an istream extraction operator (>>) should be the reverse of the ostream insertion operator (<<). In other words, if your operator<< outputs X, Y, and Z in that order, your operator>> should attempt to read X, Y, and Z in the same order.

If you don't understand the above comments, start with something simpler: like a 1-D array. Work to understand that first, before you try to work with a 2-D matrix.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.