iceColdFire said:
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