. Advertisement .
..3..
. Advertisement .
..4..
I’m building a new program, but when I run it, an error pops up. The error displayed is as follows:
Error: expression must be a modifiable value
I have tried several workarounds, but they still do not get the desired results. If you have come across this situation and have a solution for the “c++ expression must be a modifiable lvalue” problem, pls let me know. Here is what I do:
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;
}
}
if ( match == 0 )
Thanks!
The cause: Because a cast does not yield an lvalue – a value that can be utilized on the left side of an assignment. This error message is displayed.
Solution:
Because && has higher precedence than the assignment operator, your condition is similar to:
You cannot assign it to the left-hand side of this because it is a rvalue, namely the boolean returned by the evaluation of the subexpression
match == 0 && k
.In contrast, comparison takes precedence, therefore the equivalent of
match == 0 && k == m
is:If you declare a in C, you’ll also get the same error.
Then, assign a value to the index position without specifying an index position.
Doing:
You’re specifying the accessible/modifiable address previously declared.