需要左值

发布于 2024-09-27 05:58:55 字数 26 浏览 1 评论 0原文

错误消息“需要左值”实际上是什么意思?

what does the error message "Lvalue required" actually mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尝蛊 2024-10-04 05:58:55

左值是可以出现在赋值左侧的东西,换句话说,“可以分配的东西”

因此,寻找左侧不可“分配”的赋值,例如,简单的东西这可能会触发这样的错误

if (0 = foo)
{

}

在这里,您由于意外使用 = 而不是 == 而尝试分配给常量,

另请参阅

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

if (0 = foo)
{

}

Here you've got an attempt to assign to a constant because of accidentally using = rather than ==

See also

睡美人的小仙女 2024-10-04 05:58:55

这意味着实现需要一个对象,但您只是传递了一个值或函数。对于传递非左值的赋值或应用于非函数的地址操作,会发生这种情况。

左值代表“位置值”,表示引用声明为寄存器或内存位置的对象的表达式。诸如 42 之类的值与这两个条件都不匹配。更正式地说,左值分为三类

  • : 引用对象。这包括声明为 const 的对象。这些是不可修改的左值。
  • 函数指示符:指函数。 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 like 42 is a value that matches neither criteria. More formally there are three categories

  • Lvalues: Referring to objects. This includes objects declared const. Such are non-modifiable lvalues.
  • Function designators: Referring to functions. printf is a function designator, but &printf is not, while *&printf is again.
  • Others: Sometimes called "rvalue" and by the Standard described as "the value of an expression". Examples are var + 0 (yielding a value not associated with objects anymore), or an enumerator of an enumeration. &printf belongs to this category.
蓝色星空 2024-10-04 05:58:55

C99 标准规定 (6.3.2.1):


左值是具有对象类型或除 void 之外的不完整类型的表达式;如果左值在求值时未指定对象,则行为未定义。当一个对象被认为具有特定类型时,该类型由用于指定该对象的左值来指定。可修改的左值是不具有数组类型、不具有不完整类型、不具有 const 限定类型的 左值,并且如果它是结构体或union,不具有 const 限定类型的任何成员(递归地包括所有包含的聚合或联合的任何成员或元素)。

名称 lvalue 最初来自赋值表达式 E1 = E2,其中左操作数 E1 需要是(可修改的)左值。也许最好将其视为表示对象“定位器值”。有时称为rvalue 的内容在此国际标准中被描述为“表达式的值”。


换句话说,左值是您可以找到可能发生更改的内容。可修改的左值是您实际上允许更改的值。

例如,C 语句:

x = 7;

有效,因为 x 是左值。另一方面,语句:

14 = 7;

无效,因为 14 不是您可以为作业找到的内容。

代码片段:

const int x = 7;

实际上创建了一个名为 xlvalue,即使您不允许更改它(它不是可修改的“左值”)。

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 an lvalue 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 the lvalue used to designate the object. A modifiable lvalue is an lvalue 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 expression E1 = E2, in which the left operand E1 is required to be a (modifiable) lvalue. It is perhaps better considered as representing an object "locator value". What is sometimes called rvalue 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 modifiable lvalue is one that you're actually allowed to change.

For example, the C statement:

x = 7;

is valid because x is an lvalue. On the other hand, the statement:

14 = 7;

is not valid because 14 is not something you can locate for an assignment.

The snippet:

const int x = 7;

actually creates an lvalue called x even though you're not permitted to change it (it's not a modifiable `lvalue).

人海汹涌 2024-10-04 05:58:55

如果您编写像 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.

Oo萌小芽oO 2024-10-04 05:58:55

这很可能意味着您试图为无法分配的内容分配一个值。例如,以下两种情况都可能导致该错误:

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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文