0

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().

Dominik
  • 9
  • 2
  • `double Matrix::operator() (int y, int x)` -- Just to let you know, this function fails to return a value after the `cout`. Undefined behavior will occur. – PaulMcKenzie Dec 12 '19 at 19:39
  • Why do you have `operator <<` defined outside the `Matrix` class, but `operator >>` is defined in the `Matrix` class? And why isn't `operator >>` a `friend` function also? – PaulMcKenzie Dec 12 '19 at 19:43
  • 1
    Recommended reading: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 Dec 12 '19 at 19:46
  • 1
    please one question per question. Once you got one of the right you probably also know how to get the others – 463035818_is_not_an_ai Dec 12 '19 at 19:47
  • [Slightly less recommended reading](https://isocpp.org/wiki/faq/operator-overloading#matrix-subscript-op). You don't want to break encapsulation and expose the `matrix` member. It allows too much potential for accidental breakage. – user4581301 Dec 12 '19 at 19:47
  • @PaulMcKenzie couse I'm try to use few different ways to do it. I don't know which is the best. I hope somebody say me what is better – Dominik Dec 12 '19 at 19:49
  • https://tio.run/##dZBNDoIwEIX3nGKiiUD8OQBFlp7CxGBpsQl2SCkJxnB27NhGNMQupsm8ee@blrftvuZ8WivNm74SkCvsrBHlvYjmnusoXReTu3tu4YQIzwjc6WyVZV6F4cKiRRNbYUqLBpIUkp/pFDjqzoYgOkbY3miXA1uIs9jVgb3F0edKo4SufHxYEjYzIc8D4aNh2@0ChVbeyHRJczPklAda/w9MLWFFEWAfTRHsL8ap5PnGjGxS2sK9VJp@x1soQLL5G7kTyedbZIcjyGRVI1bXh1il37PY2/dbqMRnHbNonG6iafAF – jxh Dec 12 '19 at 19:58

0 Answers0