I am writing a clock class and am working on the operator overloading to compare two clocks, but my compiler is telling me:
"bool operator < (const Clock& c1, const Clock& c2) must take exactly one argument"
Does anyone know why?
I am writing a clock class and am working on the operator overloading to compare two clocks, but my compiler is telling me:
"bool operator < (const Clock& c1, const Clock& c2) must take exactly one argument"
Does anyone know why?
"In theory, there is no difference between theory and practice. But, in practice, there is."
- Jan L.A. van de Snepscheut
For a member variable:
For a global function:Code:class Clock { public: bool operator<(const Clock &rhs); private: unsigned long m_time; }; bool Clock::operator<(const Clock &rhs) { if(rhs.m_time <= m_time) return false; return true; }
Either of those should work, but I didn't actually test them so if it doesn't post the error message.Code:class Clock { public: friend bool operator<(const Clock &lhs, const Clock &rhs); private: unsigned long m_time; }; bool operator<(const Clock &lhs, const Clock &rhs) { if(rhs.m_time <= lhs.m_time) return false; return true; }
When you overload, you can change what the operator does, but you can't change its syntax. So the < operator always takes two operands, overloaded or not. But when you overload it as a member function, the class object to which it belongs is implicitly the first operand, so the function can only take one operand as a parameter.Does anyone know why?
On the other hand, if you overload it as a top-level function, you have to provide two operands as parameters.