0
votes

I have a problem. I have implemented a custom operator* as a member function.

In header:

class Matrix
{
public:
Matrix operator*(int arg); //(1)
...
}

Matrix operator*(int a, const Matrix& m)
{
    return m * a; //(2)
}

(1) I can do this in main.cpp:

Matrix a = Matrix::GetRandom.....
Matrix b = a * 2;

(2) On this line, I'm getting a compiler error:

IntelliSense: no operator "*" matches these operandsnoperand types are: const Matrix * int

How do I fix it?

2
You could also write both operators as (non-member) friend functions friend Matrix operator(Matrix, int); and friend Matrix operator(int, Matrix);. This keeps both symmetrical and they're declared next to each other.dyp

2 Answers

3
votes

m is const, so only const method can be called on it. Make Matrix::operator* a const member function:

Matrix operator*(int arg) const;
1
votes

You're missing a const in your operator overload declaration:

Matrix operator*(int arg) const;