Operator overloading

The compiler shows error while overloading << and >> operator, can someone help me removing the error please...thanks.



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

Matrix::Matrix(int m, int n)
{
init(m,n);
}

//destructor to delete the used dynamic memory.
void Matrix::init(int m, int n)
{
rows=m;
cols=n;
data= new double[m*n];
for(int i=0; i<m*n; i++)
data[i]=0;
}

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

int Matrix::getCol()
{
return cols;
}

double& Matrix::operator ()(int r, int c)
{
if (r <0 || r> rows)
{
cout<<"Illegal row index";
return data[0];
}
else if (c <0 || c > cols)
{
cout<<"Illegal Column Index:";
return data[0];
}
else return data[r*cols+c];
}


ostream& operator<<(ostream& os, Matrix &m)
{
int mval=m.getRow();
int nval=m.getCol();
for(int i=0; i<mval; i++){
for(int j=0; j < nval; j++)
os<<m(i,j)<<" ";
os<<endl;
}
return os;
}

istream& operator >> (istream& in, Matrix& k)
{
int mval=k.getRow();
int nval=k.getCol();
for(int i=0; i<mval; i++)
{
for(int j=0; j < nval; j++)
in>>k(i,j)>>" ";
}
return in;
}

//To compute the addition
Matrix Matrix::operator+(Matrix& a)
{
Matrix sum(rows, cols);
for (int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
sum(i,j) = (i,j) + a(i,j);
return sum;
}
}
}

//To compute the multiplication
Matrix Matrix::operator*(Matrix& a)
{
Matrix product(rows, cols);
for (int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
product(i,j) = (i,j) * a(i,j);
return product;
}
}
}

//To compute the substraction
Matrix Matrix::operator-(Matrix& a)
{
Matrix difference(rows, cols);
for (int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
difference(i,j) = (i,j) - a(i,j);
return difference;
}
}
}



int main(){

//the array of matrix a
Matrix a(2,2);
a(1,1)=5.0;
a(0,0)=6.0;
a(0,1)=7.0;
a(1,0)=8.0;


cout<<"matrix a\n";
cout<<a<<endl;

//the array of matrix b
Matrix b(2,2);
b(0,0) = 5.0;
b(0,1) = 5.0;
b(1,0) = 5.0;
b(1,1) = 5.0;
cout<<"matrix b\n";
cout<<b<<endl;

Matrix c,d,e,f;

//to compute the operation of the matrix
c=a+b;
d=a*b;
e=a-b;


cout<<"matrix a+b \n";
cout<<c<<endl;
cout <<"\n\n";
cout<<"matrix a*b\n";
cout<<d<<endl;
cout<<"matrix a-b\n";
cout<<e<<endl;



system("pause");
return 0;
}
in>>k(i,j)>>" ";
You can't extract into a string literal. Just remove the underlined lines.

There are several other syntax errors. If you can't fix them then post your updated code. PLEASE USE CODE TAGS. Highlight the code and click the <> button to the right of the edit window.
Topic archived. No new replies allowed.