C++

C++ Error: Expression Must Be A Modifiable Lvalue

While writing the code, various errors may occur at the time of execution. There are various types of errors like logical errors, syntax errors, and compile time errors. Logical errors are difficult to check because no error is displayed in these types of errors while the syntax and compile time errors can be detected at the time of execution. In this guide, we will also discuss about the “error: lvalue required as left operand of assignment” type of error. This error mostly occurs while comparing two variables or integers. This type of error occurs due to a misconception or mistake while writing the code. Sometimes, the newbie programmers have misconceptions about assignments and comparison operators due to which they face this type of error. Sometimes, while coding, the programmer make mistakea and uses the assignment operator instead of the comparison operator. This is easy to resolve because in the output portion, the description of the error which is lvalue is required.

Syntax:

There is no such specific syntax for this because it is not a part of the code. It is the bug that is displayed in the output area when we execute the code. The following is an example on how this error looks like:

error: lvalue required as left operand of assignment

In the previously-mentioned error, the lvalue means that the object that may have an identified memory location. We can also say that it has a specified address in the memory where it is stored. When we talk about assignment, it can store the data.

Example 1:

Now, we implement an example in which we generate this type of error and resolve it. We will first include the iostream header file that enables the compiler to perform the I/O operations. After that, we create three integer type of variables inside the main method. The first integer type is “a” to which we assign the value “2”.  We assign the value “3” to the second variable “b” same as the third variable which is “c” where it holds the value “3”. After that, we use an if() statement to compare the variables. We pass two conditions: the first condition is that variable “A” is not equal to the variable “b”. The second condition is that the variable “b” is equal to the variable “c”. If both statements are true, it displays the “comparison matches” message. Lastly, we return the null value.

#include <iostream>

using namespace std;

int main()

{

int a = 2;

int b = 3;

int c = 3;

if (a != b && b = c)

{

std::cout << "Comparison match" << std::endl;

}

return 0;

}

After the execution of the code, we get an error which displays that there is an error that shows “lvalue required as left operand of assignment”. As we know, the error occurs on line and the type of error also indicates that there is a left value (lvalue) that is required on the left of the operand to which it is pointing. Now, we resolve this error by replacing the assignment operator “=” with the comparison operator “= =”. Then, we execute the code again.

After executing the code again, we get this output which shows that we executed the code without having any error. It shows that our conditions is true.

Example 2:

Let us perform another example to get a better idea for resolving these errors. In this example, we will create three variables and, having the mode of two variables, we then compare it to the third one using the if-else statement. If the comparison matches, it moves inside the if statement. If it doesn’t match, it executes the else statement. Let us have a look at our code in which we first include our header files then move to our main function where we declare the three variables named “val1”, “val2”, and “val3” to which we assign the values “2”, “3”, and “5”. After that, using the if statement, we compare the mode of “val1” and “val2” variables with the third variable, “val3”. If this matches, our if statement inner code is executed. If it is not true, the else statement is executed. At the end, we execute our code by returning the null value.

#include <iostream>

using namespace std;

int main() {

int val1 = 2;

int val2 = 3;

int val3 = 5;

if ((val1%val2) = val3)

{

std::cout << "Comparison is a match" << std::endl;

}

else

{

   std::cout << "Comparison doesn't match" << std::endl;

}

return 0;

}

After executing our code, we discovere this type of error which indicates that we missed the lvalue as the left operand of the assignment indicates the variable “val3”. This means that the missing lvalue is on the left of the “val3” variable. Since the comparison operator is not utilized in this instance and the assignment operator is used instead, an error occurs. One more thing to keep in mind is that we cannot assign the value to any variable within the if() statement. The if() statement is used for the comparison of the variables and not for assigning the values to the variable. That is why it displays this error. Now, we resolve this problem by replacing the assignment operator with the comparison operator. Then, we re-execute the code.

After the re-execution of our code, we display the message – “comparison doesn’t match”. The reason for getting this message is that the else statements are executed as seen in the previous code. We create three variables to which we assign the values and the mode of two values is compared with the third one. Here, the mode of “val1” and “val2” is “0.6”, comparing it with the val3 which holds the number “5”. It is not true that the value of “val3” is equal to “0.6”. The compiler ignores the if statement inner code and executes the else statement.

Conclusion

In this tutorial, we discussed the compile time error that most probably occurs due to a misconception or mistake which is done by the programmer. We explained the error with a brief description and with multiple examples to make this easy for you to have an idea about it and the method to resolve and understand the error type. We made every effort to make it as simple as possible for you to fix this error whenever it pops up in your code as you’re writing a program.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.