overloading < and >

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johnmmcparland

    #1

    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

  • p.lepin@ctncorp.com

    #2
    Re: overloading &lt; and &gt;

    On Mar 26, 5:05 pm, "johnmmcparland "
    <johnmmcparl... @googlemail.com wrote:
    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

    Comment

    • Rolf Magnus

      #3
      Re: overloading &lt; and &gt;

      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.

      Comment

      • johnmmcparland

        #4
        Re: overloading &lt; and &gt;

        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!


        Comment

        Working...