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;
};
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;
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"
#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.
Also note the other two modifications I made to your code:
-istream and ostream belong to the std namespace. Unless you use usingnamespace 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.
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.