Rickie Rickie - 3 months ago 34
C++ Question

binary 'operator' : no operator defined which takes a left-hand operand of type 'type' (or there is no acceptable conversion)

I have know idea why I get this error because I did implement the

operator ==
of
Fraction
. Even when I change
f==0
to
f==Fraction(1)
, it fails to compile.

#include <iostream>

using std::cout;

class Fraction
{
public:
Fraction(int a, int b = 1) {}
bool operator == (const Fraction&) { return true; }
};

template <typename T>
class Bar
{
public:
bool test(const T& f) { return (f==0);} // ERROR
};

int main()
{
Bar<Fraction> f;
f.test(3);
}

Answer

You need to make the operator== member function of Fraction a const member function. Otherwise, it cannot be used with LHS which is const in this expression:

(f==0)
Comments