C++ 表达式必须是可修改的左值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12745601/
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
Expression must be a modifiable lvalue
提问by E_learner
I have this following code:
我有以下代码:
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
错误:表达式必须是可修改的值
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:
在那条“如果”行上。我不是要在这里修改“匹配”或“k”值,但为什么会出现此错误?如果我只这样写:
if ( match == 0 )
it is ok. Could someone explain it to me?
没关系。有人可以向我解释一下吗?
回答by Kerrek SB
The assignment operator has lower precedence than &&
, so your condition is equivalent to:
赋值运算符的优先级低于&&
,因此您的条件等效于:
if ((match == 0 && k) = m)
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.
但是它的左侧是一个右值,即对 subexpression 求值所产生的布尔值match == 0 && k
,因此您不能对其进行赋值。
By contrast, comparison has higher precedence, so match == 0 && k == m
is equivalent to:
相比之下,比较具有更高的优先级,因此match == 0 && k == m
等效于:
if ((match == 0) && (k == m))
回答by Alan
In C, you will also experience the same error if you declare a:
在 C 中,如果你声明一个:
char array[size];
and than try to assign a value without specifying an index position:
然后尝试在不指定索引位置的情况下分配值:
array = 'array[index] = '0\';
';
By doing:
通过做:
% g++-trunk -Wall -c ederman.cc
ederman.cc: In function ‘void foo()':
ederman.cc:9:30: error: lvalue required as left operand of assignment
if ( match == 0 && k = M )
^
You're specifying the accessible/modifiable address previously declared.
您正在指定先前声明的可访问/可修改地址。
回答by Basile Starynkevitch
Remember that a single =
is always an assignment in C or C++.
请记住,单个=
始终是 C 或 C++ 中的赋值。
Your test should be if ( match == 0 && k == M )
you made a typo on the k == M
test.
您的测试应该是if ( match == 0 && k == M )
您在k == M
测试中打错了字。
If you really mean k=M
(i.e. a side-effecting assignment inside a test) you should for readability reasons code if (match == 0 && (k=m) != 0)
but most coding rules advise not writing that.
如果您的意思是k=M
(即测试中的副作用分配),出于可读性原因,您应该编写代码,if (match == 0 && (k=m) != 0)
但大多数编码规则建议不要编写该代码。
BTW, your mistake suggests to ask for all warnings (e.g. -Wall
option to g++
), and to upgrade to recent compilers. The next GCC 4.8 will give you:
顺便说一句,你的错误建议要求所有警告(例如-Wall
选项g++
),并升级到最近的编译器。下一个 GCC 4.8 将为您提供:
and Clang 3.1 also tells you ederman.cc:9:30: error: expression is not assignable
Clang 3.1 也告诉你 ederman.cc:9:30: error: expression is not assignable
So use recent versions of free compilers and enable all the warnings when using them.
因此,请使用最新版本的免费编译器并在使用它们时启用所有警告。
回答by tomahh
You test k = M
instead of k == M
.
Maybe it is what you want to do, in this case, write if (match == 0 && (k = M))
你测试k = M
而不是k == M
.
也许这是你想做的,在这种情况下,写if (match == 0 && (k = M))