unknown operator

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?
why does operator= return bool here? Did you mean to overload operator==?
why wouldn't it return bool? assignment operator for standard built in classes return bool by default.
Uh, no they don't? They return the type as a reference. >_>
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
then why can you int a; if (a = 10); else;?
closed account (3hM2Nwbp)
Precedence assigns 10 to `a`, then `a` is evaluated by the if statement.
@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?
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
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
anywho, there's nothing wrong with me overriding my class operator ='s to return bool is there?
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
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
Topic archived. No new replies allowed.