How to overload >> operator?

Hey guys, I've been working on this assignment where I have to implement a class Matrix in which I have to take input from user by overloading >> operator. The problem is that I can't define the >> operator in Matrix.h nor in Matrix.cpp because it gives an error on compilation. Help me out please.

This is my Matrix.h file

#ifndef MATRIX_H
#define MATRIX_H
class Matrix
{
public:
Matrix();
Matrix(Matrix &obj);
Matrix(int row, int col);
~Matrix();
friend istream &operator >> (istream &is, Matrix &obj);
friend ostream &operator << (ostream &os, Matrix &obj);
void SetMatrix(int Fill, int row, int col);
void AllocateRows();
void AllocateCols(int i, int j);
int getRows();
int getCols();
int GetMatDat(int i, int j);
private:
int **MatrixData;
int Columns;
int Rows;
};

#endif



and this is my Matrix.cpp file

#include "Matrix.h"
#include <iostream>
#include <conio.h>

using namespace std;

Matrix::Matrix()
{
MatrixData = nullptr;
Columns = 1;
Rows = 1;
}

Matrix::Matrix(int rows, int col)
{
MatrixData = nullptr;
MatrixData = new int *[rows];
for (int i = 0; i < rows; i++)
{
*(MatrixData + i) = new int[col];
}
}

Matrix::Matrix(Matrix &obj)
{
this->MatrixData = new int *[obj.Rows];

for (int i = 0; i < obj.Rows; i++)
{
*(this->MatrixData) = new int[obj.Columns];
for (int j = 0; j < obj.Columns; j++)
{
*((this->MatrixData + i) + j) = *((obj.MatrixData + i) + j);
}
}
}

Matrix::~Matrix()
{
for (int i = 0; i < Rows; i++)
delete[](MatrixData + i);
delete[]MatrixData;
}

void Matrix::SetMatrix(int Fill, int row, int col)
{
*(*(this->MatrixData + row) + col) = Fill;
}

void Matrix::AllocateRows()
{
this->MatrixData = new int *[this->Rows];
}

void Matrix::AllocateCols(int i, int j)
{
*(this->MatrixData + i) = new int[j];
}

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

int Matrix::getCols()
{
return Columns;
}

int Matrix::GetMatDat(int i, int j)
{
return *(*(this->MatrixData + i) + j);
}

istream &operator >> (istream &is, Matrix &obj)
{
int rows = 0;
int cols = 0;
int temp = 0;

rows = obj.getRows;
cols = obj.getCols;
obj.AllocateRows();

for (int i = 0; i < rows; i++)
{
obj.AllocateCols(i, cols);

for (int j = 0; j < cols; j++)
{
cout << "Enter for : " << i << "th row and " << j << "th coloumn.\n";
cin >> temp;
obj.SetMatrix(temp, i, j);
}
}

}

ostream &operator << (ostream &os, Matrix &obj)
{
int rows = 0;
int cols = 0;

rows = obj.getRows();
cols = obj.getCols();

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << obj.GetMatDat(i, j) << " ";
}

cout << endl;
}
}
Protip:
You can click on Edit Topic and you'll find a way to change which section your thread is in, so you don't have to make duplicate threads.
Also, code tags: http://www.cplusplus.com/articles/jEywvCM9/

Remember the compiler has to know about the types. In your header file"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

 #ifndef MATRIX_H
 #define MATRIX_H
 class Matrix
 {
 public:
 Matrix();
 Matrix(Matrix &obj);
 Matrix(int row, int col);
 ~Matrix();
 friend istream &operator >> (istream &is, Matrix &obj);
 friend ostream &operator << (ostream &os, Matrix &obj);
 void SetMatrix(int Fill, int row, int col);
 void AllocateRows();
 void AllocateCols(int i, int j);
 int getRows();
 int getCols();
 int GetMatDat(int i, int j);
 private:
 int **MatrixData;
 int Columns;
 int Rows;
 };

 #endif 


You make use of istream and ostream but you forget to include the proper header.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 #ifndef MATRIX_H
 #define MATRIX_H

#include <iostream>

 class Matrix
 {
 public:
 Matrix();
 Matrix(Matrix &obj);
 Matrix(int row, int col);
 ~Matrix();
 friend std::istream &operator >> (std::istream &is, Matrix &obj);
 friend std::ostream &operator << (std::ostream &os, const Matrix &obj);
//... 


Also note the other two modifications I made to your code:
-istream and ostream belong to the std namespace. Unless you use
using namespace std, you must fully qualify the name. (Prefer my modification over using namespace in header files)
-I changed the prototype of your insert operator to use a reference to a constant object. When you print the matrix, you shouldn't be modifying it in any way, so keep in mind "const-correctness".

Also, this is pretty personal, but I really don't like making the insert and extract operators friends of the class, despite it being the classic example in many books and tutorials. I prefer to keep them as non-friends and take advantage of the pretty interface I gave the class.
Last edited on
I used the iostream and using namespace std in the header file but again it gives the same error. error says that I am missing ';' before '&' in 13th line of my header file.. plus I got 30 other errors all related to this operator and the ostream operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef MATRIX_H
 #define MATRIX_H

#include <iostream>
using namespace std;
 class Matrix
 {
 public:
 Matrix();
 Matrix(Matrix &obj);
 Matrix(int row, int col);
 ~Matrix();
 friend std::istream &operator >> (std::istream &is, Matrix &obj);
 friend std::ostream &operator << (std::ostream &os, const Matrix &obj);
//... 
Last edited on
Strange... I don't get that error. Check out: http://ideone.com/MBMQAB

Can you try a clean build?
Strange! Then there must be an issue with my compiler. Let me build again. and thanks alot for your help man.
Topic archived. No new replies allowed.