简体   繁体   English

错误:左值需要作为增量操作数

[英]error: lvalue required as increment operand

I tried the following code:我尝试了以下代码:

int a = ++2;

I am aware that a constant cannot be modified, but I cannot understand the error that the compiler outputs:我知道无法修改常量,但我无法理解编译器输出的错误:

*  error: lvalue required as increment operand 

What exactly does it mean?究竟是什么意思?

An expression is considered an "lvalue" when it designates a modifiable location in memory.当一个表达式在内存中指定一个可修改的位置时,它被认为是一个“左值”。 Operators which modify a value require that their operand is an lvalue.修改值的运算符要求其操作数是左值。

Assignment operators require that the left-hand side is an lvalue.赋值运算符要求左侧是左值。 This is where this jargon originates from.这就是这个行话的起源。 Postfix and prefix increment/decrement operators also have this requirement for their single operand.后缀和前缀递增/递减运算符对其单个操作数也有此要求。

An lvalue expression may not be just a variable name, but also a more complex expression, for example:一个左值表达式可能不仅仅是一个变量名,还可能是一个更复杂的表达式,例如:

*(p + 2); // the unary * operator produces an lvalue
*(p + 2) = 5; // so that this is possible
(*(p + 2))++; // or this

What exactly it means is that它的确切意思是

  1. ++ is by definition an increment operator (an operator that increments) ++根据定义是递增运算符递增的运算符)
  2. The thing it operates on is called the increment operand它操作的东西叫做增量操作数
  3. This thing is required to be an lvalue (something that amounts to a pointer)这个东西需要是一个左值(相当于一个指针的东西)
  4. In this case 2 is the increment operator and it is not an lvalue .在这种情况下2增量运算符,它不是左值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM