Error no match for 'operator*'

16,728

As surmised by just about everybody, the problem was missing const qualifiers. Also a factor was that a copy was being returned instead of a reference.

The invalid code in question was like this:

myClass operator *= (myClass &num) { return Mul(num); }
myClass operator * (myClass &num) { return Mul(num); }

The corrected version:

myClass& operator *= (const myClass &num) { Mul(num); return *this; }
friend myClass operator * (const myClass &num1, const myClass &num2) { myClass tmp(num1) tmp.Mul(num2); return tmp; }
Share:
16,728
John Frickson
Author by

John Frickson

Updated on June 04, 2022

Comments

  • John Frickson
    John Frickson almost 2 years

    I have a class that does decimal calculations. I have all the math operators overloaded. It works great for fairly simple calculations, but fails when I need to add parens. For example, both calculations in the following work and get the correct result:

    myClass r, a = 100000, b = 2.5, c = 10, d = 30;
    r = c / d * a * b;
    r = (c / d) * a * b;
    

    but if I change the calculation to r = a * b * (c / d); the compile fails with:

    error: no match for ‘operator*’ in ‘myClass::operator*(myClass&)((* & b)) * myClass::operator/(myClass&)((* & d))’ in gcc 4.6.2.
    

    I'm probably missing something simple, but can't find it. What am I doing wrong?