473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1701
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.