By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,184 Members | 1,306 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,184 IT Pros & Developers. It's quick & easy.

overloading < and >

P: n/a
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

Mar 26 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Mar 26, 5:05 pm, "johnmmcparland"
<johnmmcparl...@googlemail.comwrote:
bool Date::operator<(const Date& d)
{
[implementation]
}

bool Date::operator>(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.

--
Pavel Lepin

Mar 26 '07 #2

P: n/a
johnmmcparland wrote:
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)
This should be:

bool Date::operator<(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::operator>(const Date& d)
bool Date::operator>(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.

Mar 26 '07 #3

P: n/a
On 26 Mar, 15:53, Rolf Magnus <ramag...@t-online.dewrote:
johnmmcparland wrote:
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)

This should be:

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

bool Date::operator>(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.
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!
Mar 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.