“l-value required” error

2019-01-18 17:29发布

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

10条回答
淡お忘
2楼-- · 2019-01-18 17:43

Try to compile:

5 = 3;

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

查看更多
家丑人穷心不美
3楼-- · 2019-01-18 17:45

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.

查看更多
走好不送
4楼-- · 2019-01-18 17:52

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
查看更多
三岁会撩人
5楼-- · 2019-01-18 17:53

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

查看更多
beautiful°
6楼-- · 2019-01-18 17:54

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;
}
查看更多
Evening l夕情丶
7楼-- · 2019-01-18 18:00

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
查看更多
登录 后发表回答