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.