需要左值
错误消息“需要左值”实际上是什么意思?
what does the error message "Lvalue required" actually mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
错误消息“需要左值”实际上是什么意思?
what does the error message "Lvalue required" actually mean?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
左值是可以出现在赋值左侧的东西,换句话说,“可以分配的东西”
因此,寻找左侧不可“分配”的赋值,例如,简单的东西这可能会触发这样的错误
在这里,您由于意外使用 = 而不是 == 而尝试分配给常量,
另请参阅
An lvalue is something that can appear on the left side of an assignment, in other words 'something that can be assigned'
So, look for an assignment where the left hand side isn't 'assignable', for example, something as simple as this might trigger such an error
Here you've got an attempt to assign to a constant because of accidentally using = rather than ==
See also
这意味着实现需要一个对象,但您只是传递了一个值或函数。对于传递非左值的赋值或应用于非函数的地址操作,会发生这种情况。
左值代表“位置值”,表示引用声明为寄存器或内存位置的对象的表达式。诸如
42
之类的值与这两个条件都不匹配。更正式地说,左值分为三类printf
是函数指示符,但&printf
不是,而*&printf
又是。var + 0
(生成不再与对象关联的值)或枚举的枚举器。&printf
就属于这一类。It means the implementation expects an object, but you just passed a value or function. This happens for assignments you passed a non-lvalue or for address-of operations applied to non-functions.
Lvalue stands for "location value" and means an expression that refers to an object either declared as
register
or to a memory location. Something like42
is a value that matches neither criteria. More formally there are three categoriesprintf
is a function designator, but&printf
is not, while*&printf
is again.var + 0
(yielding a value not associated with objects anymore), or an enumerator of an enumeration.&printf
belongs to this category.C99 标准规定 (6.3.2.1):
左值是具有对象类型或除 void 之外的不完整类型的表达式;如果左值在求值时未指定对象,则行为未定义。当一个对象被认为具有特定类型时,该类型由用于指定该对象的左值来指定。可修改的左值是不具有数组类型、不具有不完整类型、不具有 const 限定类型的
左值
,并且如果它是结构体或union,不具有 const 限定类型的任何成员(递归地包括所有包含的聚合或联合的任何成员或元素)。名称
lvalue
最初来自赋值表达式E1 = E2
,其中左操作数E1
需要是(可修改的)左值
。也许最好将其视为表示对象“定位器值”。有时称为rvalue
的内容在此国际标准中被描述为“表达式的值”。换句话说,
左值
是您可以找到可能发生更改的内容。可修改的左值
是您实际上允许更改的值。例如,C 语句:
有效,因为 x 是左值。另一方面,语句:
无效,因为
14
不是您可以为作业找到的内容。代码片段:
实际上创建了一个名为
x
的lvalue
,即使您不允许更改它(它不是可修改的“左值”)。The C99 standard states (6.3.2.1):
An
lvalue
is an expression with an object type or an incomplete type other than void; if anlvalue
does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by thelvalue
used to designate the object. A modifiablelvalue
is anlvalue
that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.The name
lvalue
comes originally from the assignment expressionE1 = E2
, in which the left operandE1
is required to be a (modifiable)lvalue
. It is perhaps better considered as representing an object "locator value". What is sometimes calledrvalue
is in this International Standard described as the "value of an expression".In other words, an
lvalue
is something that you can locate for potentially changing. A modifiablelvalue
is one that you're actually allowed to change.For example, the C statement:
is valid because
x
is anlvalue
. On the other hand, the statement:is not valid because
14
is not something you can locate for an assignment.The snippet:
actually creates an
lvalue
calledx
even though you're not permitted to change it (it's not a modifiable `lvalue).如果您编写像
function(parameter) = value;
这样的代码,就会出现错误,因为您无法为任何不可能容器的内容分配值。the error cometh if you code somrthing like
function(parameter) = value;
because you cannot assign value to anything that is not a possible container for it.这很可能意味着您试图为无法分配的内容分配一个值。例如,以下两种情况都可能导致该错误:
5 = 5;
myObject->myMethod() = 5;
Most likely it means that you tried to assign a value to something that cannot be assigned to. For example, both of the following would probably cause that error:
5 = 5;
myObject->myMethod() = 5;