“l-value required” error

早过忘川 提交于 2019-11-27 03:41:49

问题


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


回答1:


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.




回答2:


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




回答3:


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)




回答4:


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.




回答5:


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.




回答6:


Try to compile:

5 = 3;

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




回答7:


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.




回答8:


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



回答9:


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



回答10:


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;
}


来源:https://stackoverflow.com/questions/1353384/l-value-required-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!