overloading < and >

J

johnmmcparland

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::eek:perator<(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::eek:perator>(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
 
P

p.lepin

bool Date::eek:perator<(const Date& d)
{
[implementation]

}

bool Date::eek:perator>(const Date& d)
{
return (d < (*this));
}

but my compiler informs me that those operators cannot be
overloaded.

I'm not sure what's the problem with your compiler, but
you're trying to use a const reference in a non-const
context. After I made those operators const, g++ 4.1.2
compiled the test code without problem.
 
R

Rolf Magnus

johnmmcparland said:
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::eek:perator<(const Date& d)

This should be:

bool Date::eek:perator<(const Date& d) const
{
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::eek:perator>(const Date& d)

bool Date::eek:perator>(const Date& d) const
{
return (d < (*this));
}

but my compiler informs me that those operators cannot be overloaded.

What's the exact error message, and to which line does it belong?
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.

They can be overloaded.
Could someone explain how I should overload these operators (and also
the >= and <= operators) and why I get this compiler error?

You didn't give enough information. You should give a minimal, but complete
program along with the error message produced by the compiler.
 
J

johnmmcparland

This should be:

bool Date::eek:perator<(const Date& d) const



bool Date::eek:perator>(const Date& d) const



What's the exact error message, and to which line does it belong?


They can be overloaded.


You didn't give enough information. You should give a minimal, but complete
program along with the error message produced by the compiler.

Hmm my previous post doesn't seem to have got here.

In it I had explained my problem much clearer and provided a nice
compact example but in the process of retyping I found the problem I
was having was to do with the fact I defined the < operator twice in
my header file. D'oh!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top