Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Operator overloading error !!!

Reply
Thread Tools

Operator overloading error !!!

 
 
iceColdFire
Guest
Posts: n/a
 
      05-28-2005
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

 
Reply With Quote
 
 
 
 
Rapscallion
Guest
Posts: n/a
 
      05-28-2005
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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-28-2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() gob00st@googlemail.com C++ 2 02-21-2009 04:26 AM
overloading operator->*() and operator->() gob00st@googlemail.com C++ 11 02-20-2009 08:52 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments