Hi all,
I know it is possible to overload the operators and < in C++ but how
can I do this.
Assume I have a class Date with three int members, m_day, m_month and
m_year. In the .cpp files I have defined the < and operators;
bool Date::operator<(const Date& d)
{
return
// this year < d year
(m_year < d.m_year) ||
// this year == d year and this month < d month
(m_year == d.m_year && m_month < d.m_month) ||
// this year == d year and this month == d month but
// this day < d day
(m_year == d.m_year && m_month == d.m_month &&
m_day < d.m_day);
}
bool Date::operator>(const Date& d)
{
return (d < (*this));
}
but my compiler informs me that those operators cannot be overloaded.
I will assume this is because I have not used the correct syntax as a
quick Google search on operator overloading will quickly show you that
these operators are not two of the few you cannot overload.
Could someone explain how I should overload these operators (and also
the >= and <= operators) and why I get this compiler error?
Thanks,
John