When do we get "l-value required" error...while compiling C++ program???(i am using VC++ )
This happens when you're trying to assign to something (such as the result of a scalar function) that you can't assign to.
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.
Try to compile:
5 = 3;
and you get error: lvalue required as left operand of assignment
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.
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)
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 expressiona = 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.