Matrix.h
class Matrix {
private:
int row; // rows
int col; // columns
public:
double** matrix; // pointer to this matrix
int getRow();
int getCol();
void randValues();
double operator()(int y, int x);
friend std::ostream& operator<< (std::ostream& stream, Matrix& matrix);
istream& operator>>(istream& input, Matrix &matrix){
for(int i=0; i<row; i++){
for(int k=0; k<col; k++){
input>>matrix.matrix[i][k];
}
}
return input;
}
};
For operator >> code::blocks say: include\Matrix.h|50|error: 'std::istream& Matrix::operator>>(std::istream&, Matrix&)' must take exactly one argument|
Matrix.cpp
ostream& operator <<(ostream& stream, Matrix& matrix){
for (int i=0; i < matrix.getRow(); i++){
for (int k=0; k < matrix.getCol(); k++){
stream << matrix.matrix[i][k] << " " ;
}
stream << '\n';
}
return stream;
}
double Matrix::operator() (int y, int x)
{
if( y < 0 || x < 0 || y > this->row || x > this->col)
{
cout << "Wrong index of Matrix()";
}
else
{
return this->matrix[y][x];
}
}
For operator << code blocks say: Matrix.cpp|432|multiple definition of `operator<<(std::ostream&, Matrix&)'|
For operator () when I use it in main(), code blocks say 'matx' cannot be used as a function|
int main(){
Matrix *matx = new Matrix(5,5);
matx->randValues;
cout << matx(0,0);
}
I would like to ask how to corectly declare operator overloading, how to implement operator overloading and then how to use it in main().