0

I'm coding a small library to make operations with matrices and vectors and I have ran into a problem a bit strange.

I have overloaded my *, + and - operators to be able to easily operate with my Matrix2D objects.

typedef float MVT; // MatrixValueType

class Matrix2D {
    private:
        int rows, columns;
        MVT** values;
        int largestNumberToPrint;

    public:
        Matrix2D(const int _rows, const int _columns);
        Matrix2D(const std::vector<std::vector<MVT>> _matrixValues);
        ~Matrix2D();

        Matrix2D t();
        MVT get(int _row, int _column);
        void set(int _row, int _column, MVT& _value);
        void print();
}

That's basically my Matrix2D class, and this is how I have overloaded my * operator:

friend linear::Matrix2D operator*(const linear::Matrix2D& a, const linear::Matrix2D& b) {
            if(a.columns != b.rows)
                throw std::logic_error("columns of A must match rows of B");

            if(a.rows < 1)
                throw std::logic_error("rows of A is 0, no possible multiplication");    

            linear::Matrix2D _multMatrix(a.rows, b.columns);

            for(auto _row = 0; _row < _multMatrix.rows; _row++)
                for(auto _col = 0; _col < _multMatrix.columns; _col++) {
                    MVT _mulValue = 0;

                    for(auto _trace = 0; _trace < a.columns; _trace++)
                        _mulValue += a.values[_row][_trace] * b.values[_trace][_col];

                    _multMatrix.set(_row, _col, _mulValue);
                }

            return _multMatrix;
        }

And here the + operator (The - operator is the same but with - operations)

friend linear::Matrix2D operator+(const linear::Matrix2D& a, const linear::Matrix2D& b) {
            if(a.columns != b.columns || a.rows != b.rows)
                throw std::logic_error("A and B must be same size");

            if(a.rows < 1 || b.rows < 1)
                throw std::logic_error("rows of A and B can't be empty");    

            linear::Matrix2D _sumMatrix(a.rows, a.columns);

            for(auto _row = 0; _row < _sumMatrix.rows; _row++)
                for(auto _col = 0; _col < _sumMatrix.columns; _col++) {
                    MVT _mulValue = a.values[_row][_col] + b.values[_row][_col];
                    _sumMatrix.set(_row, _col, _mulValue);
                }

            return _sumMatrix;
        }

I have tested that the overloaded operator works and it does. The problem comes now, I have overloaded to the operator *=, and to avoid having to rewrite too much code I have done this:

linear::Matrix2D& operator*=(const linear::Matrix2D& _a){
      *this = *this * _a;
      return *this;
}

But I got an error in this line '*this = *this * _a;' on the * multiplication operator. "It says no operator '*' matches these operands -- operand types are: linear::Matrix2D * const linear::Matrix2D", but as I've showed you I have defined them.

And the same happens for += and -= operators:

linear::Matrix2D& operator+=(const linear::Matrix2D& _a){
            *this = *this + _a;
            return *this;
        }

And

linear::Matrix2D& operator-=(const linear::Matrix2D& _a){     
            *this = *this - _a;            
            return *this;
        }

The errors are:

"It says no operator '+' matches these operands -- operand types are: linear::Matrix2D + const linear::Matrix2D"

And

"It says no operator '-' matches these operands -- operand types are: linear::Matrix2D - const linear::Matrix2D"

capture of error log in vscode

Finally, I have to say that even that vscode shows up this error, I can compile and run this code with mingw g++, but I'm not sure why this compiles and runs if there's an error or if internally something is going wrong while running it.

I would appreciate any help, thanks.

  • 1
    I may have been hasty in closing. At a quick glance it looked as if this is a typical problem caused by binary operators being implemented in the class itself, but not being marked const. You should show actual code. You've shown only part of the class. It's missing a semi-colon. That could be the problem, even. Or the binary operator overloads are not visible at the time you define your `+=` _etc_ operators. In that case, adding function prototypes or not defining those inline with the class definition may help. – paddy Aug 08 '19 at 05:38
  • 1
    VSCode might easily be wrong about this, especially given that it compiles without errors. – r3mus n0x Aug 08 '19 at 05:40
  • Maybe using parenthesis will help: *this = (*this) * _a; – ufok Aug 08 '19 at 05:47
  • Normally you don't write +, -, * operators as friends - make them class members instead. Instead of `friend Mat operator + (const Mat& x, const Mat& y)` write `Mat operator + (const Mat& other) const`. It might resolve the problem. Generally, I think the problem is that you used an operator before you declared it but its hard to tell for sure. – ALX23z Aug 08 '19 at 06:42
  • @Scheff Did you seen this part: "even that vscode shows up this error, I can compile and run this code with mingw g++". Problem is with code assistant not with compiler. IDE (editor) live parsers are simplier then compilers, so additional parentheses may (or not) help. – ufok Aug 08 '19 at 07:08
  • @ufok Sorry, no, I didn't. (Should have read upto the very end.) I must admit that I would disable that code assistant if it would give me such stupid hints... ;-) (...or just ignore them.) – Scheff's Cat Aug 08 '19 at 07:10
  • @ALX23z **No.** Binary operators should in most cases be non-members. – L. F. Aug 08 '19 at 08:23
  • 1
    Note that it's more common to define `+` using `+=`, than the other way around. See e.g. https://en.cppreference.com/w/cpp/language/operators#Binary_arithmetic_operators – Bob__ Aug 08 '19 at 09:05
  • @L. F. do you have any good reason for that? A single advantage? Cause I see only a variety of minor disadvantages. – ALX23z Aug 08 '19 at 13:51
  • @ALX23z Partly for symmetry. In fact, I can't think of a single advantage of defining the binary operators as members (except in some edge cases). – L. F. Aug 08 '19 at 13:53
  • Okay thank you guys, I've changed from vscode to clion and it doesn't mark that as an error and everything compiles correctly and runs correctly, and the semi-colon @paddy says in the code it's my fault, I didn't copy it here but it is in my code. Also the parenthesis in vscode don't solve the problem, so must be a vscode mistake marking it as an error (?) . – borja vazquez Aug 09 '19 at 06:47

0 Answers0