I have had this problem a couple of times but i am unsure of what it means. could someone tell me what the error message "non-lvalue in assignment" translates to in english, please?

Recommended Answers

All 2 Replies

That means you are literally treating something that isn't an lValue as an lValue.

For example if a method doesn't return a reference to something, its possible that attempting to treat the method as an lvalue wont work since you're returning a copy of the actual object in which that copy may not be assigned to anything else...

It's actually hard for me to believe that you can't treat a temporary as an lValue considering potential chained = statements.

I believe this is an appropriate example--

( please excuse the code inlining - I'm in a hurry @_@ )

int method(){
    return 9;
}

int& method2(int num){
    return num;
}

int main(){

    method() = 8; // should throw an error
    int x = 10;
    method2(x) = 5; // valid since the method returns a reference which can be assigned 
    a value

}

Edit: Actually its possible that primitive types, which aren't objects, will be the most likely to flag such an error. It's possible that the assignment operator isn't valid for 'const' or temporary types (which makes sense, otherwise you could change the value of the number 9 by assigning it 5 and get the value of 5 instead of 9 wen you need 9... that would be a pain @_@ )

-Alex

Ah, now I see what I was doing wrong. That makes sence. Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.