tags:

views:

929

answers:

6

When do we get "l-value required" error...while compiling C++ program???(i am using VC++ )

+2  A: 

This happens when you're trying to assign to something (such as the result of a scalar function) that you can't assign to.

Greg Hewgill
+1  A: 

You are trying to use an invalid value for an l-value somewhere in your code. An l-value is an expression to which a value can be assigned.

For example, you might have a statement like the following:

10 = x;

where you should instead have:

x = 10;

Although it is probably not this obvious in your case.

Brandon E Taylor
Not necessarily "a memory location," really.
strager
@strager: An lvalue refers to an object. An object is a region of storage.
sellibitze
A: 

Try to compile:

5 = 3;

and you get error: lvalue required as left operand of assignment

Nicolas Viennot
+6  A: 

An "lvalue" is a value that can be the target of an assignment. The "l" stands for "left", as in the left hand side of the equals sign. An rvalue is the right hand value and produces a value, and cannot be assigned to directly. If you are getting "lvalue required" you have an expression that produces an rvalue when an lvalue is required.

For example, a constant is an rvalue but not an lvalue. So:

1 = 2;  // Not well formed, assigning to an rvalue
int i; (i + 1) = 2;  // Not well formed, assigning to an rvalue.

doesn't work, but:

int i;
i = 2;

Does. Note that you can return an lvalue from a function; for example, you can return a reference to an object that provides a operator=().

As pointed out by Pavel Minaev in comments, this is not a formal definition of lvalues and rvalues in the language, but attempts to give a description to someone confused about an error about using an rvalue where an lvalue is required. C++ is a language with many details; if you want to get formal you should consult a formal reference.

janm
The "l" also stands for "locator" as an l-value defines a storage location locator value.
sharkin
Even worse is `= 10;`
Cecil Has a Name
This definition isn't entirely correct. For example, a function returning an instance of `std::string` will return a temporary, which is an rvalue; however, that rvalue has `operator=`, and thus can appear on the left side of `=` - but, as an rvalue, it will not bind to a non-const reference. On the other hand, a `const std::string` variable is an lvalue, even though you cannot assign to it (because it's a const one).
Pavel Minaev
janm
Of course, we have now entered "language lawyer" territory, and the answer I wrote in about 60 seconds does not cover all the nuances of C++. But as a brief description of the lvalue concept I think it holds up OK.
janm
I don't think it's language lawyering to say that lvalue means "locator value" and means to refer to memory - but it's just saying truth. Writing answers to novices is pretty easy. But writing, *correct* answers to novices is pretty difficult, i think. I deleted my answer because i found it's probably just not helping him (even though i wasn't aware of something wrong in it - apart from saying that an lvalue refers to storage and not explaining why a function can be a lvalue). But i think you should still say "modifiable lvalue" instead of just "lvalue" when you talk 'bout the assignment.
Johannes Schaub - litb
(and by the way, even though your answer is factually wrong, i still upvoted it because i think *together* with R.A and Pavel's comments, it's a good one - your explanation for the didactic part, and their comments for the factual part xD).
Johannes Schaub - litb
And *please* remove "Note that you can return an lvalue from a function; for example, you can return an object that provides a operator=().". <- Only if you return a reference, you return an lvalue. Everything else will return an *rvalue*. If you tell him that, and another one (correctly) says "`string ` will work if y returns an lvalue of type `string`" then he wrongly concludes that `string y(); string ` compiles - while it of course is invalid C++.
Johannes Schaub - litb
I agree that "return an object" should be "returns a reference to an object". I was going to change that after Pavel's comment, but then I got distracted by constness and then had to do some Real Work(tm).
janm
"Writing answers to novices is pretty easy". I don't agree with this; on one hand you can end up writing long essays with a lot of detail that a novice has no context to understand, or oversimplify and say "just do it like this and it works". And like most things, answers are improved with feedback.
janm
+2  A: 

Typically one unaccustomed to C++ might code

if ((x+1)=72) ...

in place of

if ((x+1)==72) ...

the first means assign 72 to x+1 (clearly invalid) as opposed to testing for equality between 72 and (x+1)

Elemental
A: 

R Value is an expression that always appear on right side of an assignment operator Eg:

int a = 5;//here 5 is Rvalue

L Value is an expression that can either come on left or right side of an assignment.When it is in on left side it refers to a place that can hold a value.

Here a in expression a = 5 is L Value

and when appearing on right side value is read from the L Value. Since R value which does not have capability to locate any memory it cannot hold any value like LValue so

5 = 6 or 5 = a

will be compiler error.

yesraaj