表达式必须是可修改的左值 [英] Expression must be a modifiable lvalue
问题描述
我有以下代码:
int M = 3;
int C = 5;
int match = 3;
for ( int k =0; k < C; k ++ )
{
match --;
if ( match == 0 && k = M )
{
std::cout << " equals" << std::endl;
}
}
但它给出一个错误, >
But it gives out an error saying:
错误:表达式必须是可修改的值
Error: expression must be a modifiable value
在if行。我不是试图修改匹配或k值在这里,但为什么这个错误?如果我只写如下:
on that "if" line. I am not trying to modify "match" or "k" value here, but why this error? if I only write it like:
if ( match == 0 )
没问题。有人可以解释一下吗?
it is ok. Could someone explain it to me?
推荐答案
赋值运算符的优先级低于&&
所以你的条件等价于:
The assignment operator has lower precedence than &&
, so your condition is equivalent to:
if ((match == 0 && k) = m)
但是左边是一个右值,即从sub match == 0&&&
But the left-hand side of this is an rvalue, namely the boolean resulting from the evaluation of the subexpression match == 0 && k
, so you cannot assign to it.
相比之下,比较具有更高的优先级,因此 match == 0 &&& k == m
等效于:
By contrast, comparison has higher precedence, so match == 0 && k == m
is equivalent to:
if ((match == 0) && (k == m))
这篇关于表达式必须是可修改的左值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!