Operator overloading error !!!

Discussion in 'C++' started by iceColdFire, May 28, 2005.

  1. iceColdFire

    iceColdFire Guest

    Hi,

    I created a simple c++ program to overload operators,
    class Number {...}

    now when I use the class like,,,
    Number n1(15);
    Number n2(5);
    (n1+n2).num;
    (n1+10).num;
    (15+n2);

    I get error as 'Reverse iterator' in line (15+n2)...

    Kindly explain what the issue is and what is reverse iterator...

    Thanksin advance..
    a.a.cpp
     
    iceColdFire, May 28, 2005
    #1
    1. Advertising

  2. iceColdFire

    Rapscallion Guest

    iceColdFire wrote:
    > I created a simple c++ program to overload operators,
    > class Number {...}
    >
    > now when I use the class like,,,
    > Number n1(15);
    > Number n2(5);
    > (n1+n2).num;
    > (n1+10).num;
    > (15+n2);
    >
    > I get error as 'Reverse iterator' in line (15+n2)...


    The compiler kindly explains to you that you have made an error in the
    line '(15+n2)'. I guess because you have written a deficient operator+
    for class Number.

    > Kindly explain what the issue is and what is reverse iterator...
    >
    > Thanksin advance..
    > a.a.cpp
     
    Rapscallion, May 28, 2005
    #2
    1. Advertising

  3. iceColdFire wrote:
    > I created a simple c++ program to overload operators,
    > class Number {...}
    >
    > now when I use the class like,,,
    > Number n1(15);
    > Number n2(5);
    > (n1+n2).num;
    > (n1+10).num;
    > (15+n2);
    >
    > I get error as 'Reverse iterator' in line (15+n2)...
    >
    > Kindly explain what the issue is and what is reverse iterator...


    I honestly have no idea why your compiler tells you about the reverse
    iterator, but I can speculate as to why it happens. First of all, the
    reverse iterator is a nested class of many standard containers. Do you
    use any standard containers in your program? Let me note right here
    that it would be helpful to see your 'Number' class definition.

    Now, as to the reason why it happens, I guess you defined your operator+
    as a member, so it only works if the left operand of the operator+ is of
    your 'Number' class type. When you write (15+n2), the compiler cannot
    convert 15 to a Number and is forced to try using the globally defined
    operator+ for 'int' values. It can't because the right-hand operand in
    that expression is not something it can add to an 'int', so it tries to
    find some conversion from 'n2' to something that can be added to an 'int'
    and probably finds some "reverse iterator" somewhere...

    Now, if you define your operator+ for 'Number' as a non-member, the
    compiler will be able to convert 15 to a 'Number' and add two values of
    type 'Number' for you.

    Anyway, read the FAQ, I am betting that all those issues are there.

    V
     
    Victor Bazarov, May 28, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    2
    Views:
    440
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    758
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    748
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    578
  5. Replies:
    2
    Views:
    328
Loading...

Share This Page