unknown operator

Aug 31, 2011 at 9:19pm
got error: could not convert something something yada yada ->integer::number + something unknown operator something something yada yada yada yada to bool

at line:
if ( number[i] = number[j] ); of my integer class where number is a digit pointer where digit is a class with bool operator = defined as:
1
2
3
4
5
6
7
8
9
10
    bool operator = (const int & rhs)
        {
            if (rhs >= digit_range || rhs < 0)
                return false;
            digit temp(rhs);
            dig = temp.dig;
            pv = temp.pv;
            vv = temp.vv;
            return true;
        }

where dig is a bitset<4>.

whats wrong?
Aug 31, 2011 at 9:25pm
why does operator= return bool here? Did you mean to overload operator==?
Aug 31, 2011 at 9:27pm
why wouldn't it return bool? assignment operator for standard built in classes return bool by default.
Aug 31, 2011 at 9:28pm
Uh, no they don't? They return the type as a reference. >_>
Aug 31, 2011 at 9:30pm
closed account (3hM2Nwbp)
I typically thought that operator= returns a reference to an object of the same class. That's why operator= can typically be chained.

* I'm not aware of anything in the standard that declares that operator= must do this, however.
Last edited on Aug 31, 2011 at 9:35pm
Aug 31, 2011 at 9:35pm
then why can you int a; if (a = 10); else;?
Aug 31, 2011 at 9:37pm
closed account (3hM2Nwbp)
Precedence assigns 10 to `a`, then `a` is evaluated by the if statement.
Aug 31, 2011 at 9:39pm
@luc Liebersounds silly to have so many ways to do the same thing.
Wouldn't if (a = 10, a); else; do the exact same thing?
Aug 31, 2011 at 9:39pm
if (a=10)

after the assignment operator does it thing and a now has the value 10, that if statement evaluates to

if (10)

any non zero value evaluates to true. if (a=10) does not check whether the assignment succeeded or not in case that's what you thought.
Last edited on Aug 31, 2011 at 9:39pm
Aug 31, 2011 at 9:40pm
so anyways, does anyone know what I should do If I want my operator = function to return true on success and false on failure? or what exactly is my problem here anyway.

edit: @quirkyusername, yeah thats what I thought. So many newbie posts on here having if (a = X), when they really meant if (a == X), and no one ever mentioned that its still not doing what you thought that it should had that been what you actually intended it to do.
Last edited on Aug 31, 2011 at 9:44pm
Aug 31, 2011 at 9:50pm
anywho, there's nothing wrong with me overriding my class operator ='s to return bool is there?
Aug 31, 2011 at 10:06pm
I don't advise it.

The whole idea of overloading operators is to make classes behave more natually. By returning a bool instead of the type you are breaking the natual behavior which makes your class weird and confusing. It kind of defeats the point.
Last edited on Aug 31, 2011 at 10:06pm
Aug 31, 2011 at 10:50pm
I think that the problem is with yada, yada. So if you change it to something, something, it should work.
However be careful with ...
You could beep beep instead
Last edited on Aug 31, 2011 at 10:51pm
Topic archived. No new replies allowed.