Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

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)

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.

take for example

*int a=10,b=20;

int c=++(ab+1); above code will give error because inside the bracket you have a expression ont which you want to do increment operation which is not possible. So before doing doing that you have to store that value to some variable.so above code will error of "lvalue" required.

Try to compile:

5 = 3;

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

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.

We assign value to a variable. If we try to do the reverse thing then L-value errors occur.

int x,y,z;
x=1;
y=2;
z=x+y; //Correct
x+y=z; //L-value required

I had a similar issue and I found that the problem was I used a single '=' instead of a double '==' in an if statement

lvalue error:

 if (n = 100) { code } // this is incorrect and comes back with the lvalue error

correct:

if (n == 100) { code } // this resolved my issue

This happen when you try manipulate the value of a constant may it be increments or decrements which is not allowed. `

#define MAX 10
void main(){
int num;
num  = ++MAX;
cout<<num;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top