Operator overloading error !!!

I

iceColdFire

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
 
R

Rapscallion

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)...

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.
 
V

Victor Bazarov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top