C++ “需要左值”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1353384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"l-value required" error
提问by JAY G
When do we get "l-value required" error...while compiling C++ program???(i am using VC++ )
我们什么时候会收到“需要 l 值”错误...在编译 C++ 程序时???(我正在使用 VC++)
回答by janm
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.
“左值”是一个可以作为赋值目标的值。“l”代表“左”,如等号的左侧。右值是右边的值并产生一个值,不能直接赋值。如果您得到“需要左值”,则您有一个表达式,当需要左值时会生成一个右值。
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=().
做。请注意,您可以从函数返回左值;例如,您可以返回对提供 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.
正如 Pavel Minaev 在评论中指出的那样,这不是语言中左值和右值的正式定义,而是试图向那些对使用右值而需要左值的错误感到困惑的人进行描述。C++是一门有很多细节的语言;如果你想变得正式,你应该咨询一个正式的参考。
回答by Elemental
Typically one unaccustomed to C++ might code
通常一个不习惯 C++ 的人可能会编码
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)
第一种方法是将 72 分配给 x+1(显然无效),而不是测试 72 和 (x+1) 之间的相等性
回答by Greg Hewgill
This happens when you're trying to assign to something (such as the result of a scalar function) that you can't assign to.
当您尝试分配给您无法分配的某些内容(例如标量函数的结果)时,就会发生这种情况。
回答by Brandon E Taylor
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.
您正试图在代码中的某处对 l 值使用无效值。左值是可以赋值的表达式。
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.
尽管在您的情况下可能不是很明显。
回答by terion_style
take for example
以
*int a=10,b=20;
*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.
int c=++(a b+1); 上面的代码会出错,因为在括号内你有一个表达式,你想在它上面进行增量操作,这是不可能的。所以在这样做之前,你必须将该值存储到某个变量中。所以上面的代码将需要“左值”错误。
回答by Saheb Giri
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;
}
回答by Maria Fernandez Estevez
Actually, this error occurs very often when you don't put a ";" and not to declare a variable that was probably prior to the variable that gives this error, not to declare throws this error since it cannot assign a value to a variable that was not declared ... smart boy!
实际上,当你不放“;”时,这个错误经常发生。并且不要声明一个可能在产生此错误的变量之前的变量,不要声明会引发此错误,因为它无法为未声明的变量赋值……聪明的孩子!
回答by Nicolas Viennot
Try to compile:
尝试编译:
5 = 3;
and you get error: lvalue required as left operand of assignment
你得到 error: lvalue required as left operand of assignment
回答by yesraaj
R Value is an expression that always appear on right side of an assignment operator Eg:
R 值是一个始终出现在赋值运算符右侧的表达式,例如:
int a = 5;//here 5 is Rvalue
int a = 5;//这里5是右值
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.
L Value 是一个表达式,可以出现在赋值的左侧或右侧。当它位于左侧时,它指的是可以容纳值的地方。
Here
a
in expressiona = 5
is L Value
这里
a
的表达式a = 5
是 L 值
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
当出现在右侧时,从 L 值中读取值。由于 R 值不具有定位任何内存的能力,因此它不能保存任何像 LValue 这样的值
5 = 6 or 5 = a
5 = 6 或 5 = 一个
will be compiler error.
将是编译器错误。
回答by Leo
We assign value to a variable. If we try to do the reverse thing then L-value errors occur.
我们为变量赋值。如果我们尝试做相反的事情,则会发生 L 值错误。
int x,y,z;
x=1;
y=2;
z=x+y; //Correct
x+y=z; //L-value required