472,800 Members | 1,632 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,800 software developers and data experts.

overloading < and >

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
3 1658
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
0
by: Arne Schirmacher | last post by:
I want to display a MySQL database field that can contain HTML markup. If I use <esql:get-string> then I get all of the database field, but all tags are escaped which is not what I want. If I use...
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
34
by: Mark Moore | last post by:
It looks like there's a pretty serious CSS bug in IE6 (v6.0.2800.1106). The HTML below is validated STRICT HTML 4.01 and renders as I would expect in Opera, FrontPage, and Netscape. For some...
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
4
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would really like to be able to use the succinct shell...
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.