What the assignment target must identify
The assignment operator stores its right operand in the object named by its left operand. The diagnostic lvalue required as left operand of assignment means the compiler cannot find a writable object at that position. It points to the expression before =, so read that expression first. The right side can be a literal or a calculation because it supplies a value. The left side must designate storage that the program may modify.
In C++, an lvalue expression identifies an object or function. Built-in assignment requires a modifiable lvalue for its left operand. A temporary result such as count + 1 is a value, not a location. Parentheses group expressions, but they do not create storage.
error: lvalue required as left operand of assignmentMSVC reports error C2106: '=': left operand must be l-value. Later diagnostics can follow from the same invalid expression, so start with the first error line and its source location.
A literal cannot be an assignment target
A number such as 5 is a value written in the source. It does not name a variable whose stored value can change. Code that tries to replace the literal with a variable assignment therefore has no valid destination. The compiler flags the equals sign and identifies the left operand as the problem.
int main() {
int x = 4;
5 = x;
}
Put the value in a named variable first. Use that variable when you need to read or update the stored value. A variable is a modifiable lvalue unless its type or declaration prevents modification.
#include <iostream>
int main() {
int x = 4;
std::cout << x << '\n';
}
4
A calculation produces a value, not a destination
The expression a + b computes a result. C++ does not make the result a writable object just because both inputs are variables. Replacing the addition with parentheses, as in (a + b) = c, changes only grouping. The sum still has no storage location that assignment can update.
int main() {
int a = 2;
int b = 3;
int c = 8;
a + b = c;
}
To keep the sum, store it in a variable. Then assign to that variable, or use it directly on the right side of an assignment. This separates the calculation from the state change and makes the intended destination explicit.
#include <iostream>
int main() {
int a = 2;
int b = 3;
int c = 8;
int sum = a + b;
sum = c;
std::cout << sum << '\n';
}
8
A second arithmetic form uses a parenthesized increment expression. It has the same rule: the calculation is not the counter itself. Put the computed value in a named variable, then assign through that name.
#include <iostream>
int main() {
int count = 4;
(count + 1) = 9;
}
#include <iostream>
int main() {
int count = 4;
count += 1;
std::cout << count << '\n';
}
5
Assignment inside a logical condition changes the parse
A frequent typo mixes || with assignment: if (x == 1 || y = 2). Assignment has lower precedence than logical OR, so the expression parses as (x == 1 || y) = 2. The left side of that assignment is the result of ||, a temporary Boolean value. It is not an object that can receive a new value. MSVC may also emit warnings about assignment in a condition, but C2106 identifies the invalid target.
int main() {
int x = 1;
int y = 2;
if (x == 1 || y = 2) {
return 0;
}
}
If you intend to compare y with two, write y == 2. If you intend to assign before testing, place the assignment in a separate statement and test the resulting variable. Parentheses can make assignment precedence explicit, but they cannot make a logical expression assignable.
#include <iostream>
int main() {
int x = 1;
int y = 2;
if (x == 1 || y == 2) {
std::cout << "match\n";
}
}
match
A function that returns a built-in type by value is not assignable
A function that returns int by value gives its caller a separate result. Assigning to that result does not update the function's local variables or any other object. The call expression is therefore not a writable destination in ordinary built-in assignment.
int makeValue() { return 5; }
int main() {
makeValue() = 8;
}
Store the returned value in a local variable before changing it. A class type returned by value can support assignment through its member operator=; for example, a returned std::string can be assigned. That distinction is why this rule applies to built-in assignment.
#include <iostream>
int makeValue() { return 5; }
int main() {
int value = makeValue();
value = 8;
std::cout << value << '\n';
}
8
Trace the expression before changing the code
Start at the equals sign, then identify the complete left operand using C++ precedence rules. Ask whether that expression names a modifiable object. If it is a literal, arithmetic result, comparison, logical result, or by-value function result, find the object the program should actually change. Keep the calculation on the right when you only need its value. Introduce a variable when you need to retain and update that value later.
Not every assignment failure is an lvalue error. Assigning to a const object produces a diagnostic about a read-only variable in GCC and a corresponding const-assignment diagnostic in MSVC. Conditional expressions can be assignable when both alternatives are compatible lvalues; for example, a conditional can select between two modifiable integer variables. Check the types and value categories of both alternatives before relying on that form.
Exercise
Change the starter so it prints 12. The starter tries compound assignment on a calculation, which is not a writable object. Store the sum in a variable before updating it. Expected output:
12
int main() {
int a = 5;
int b = 6;
(a + b) += 1;
}
Show one possible solution
#include <iostream>
int main() {
int a = 5;
int b = 6;
int total = a + b;
total += 1;
std::cout << total << '\n';
}
Related guides
- Fix an invalid expression
- Find a missing semicolon
- Understand const member errors
- Search compiler diagnostics
Keep learning with CppPath
Continue with the full lesson Operators & Expressions. It includes a quiz and checked coding challenge available with a free account.