The error expression must be a modifiable lvalue means the compiler found something on the left side of an assignment that it can’t write to. You asked C++ to store a value somewhere, but the target isn’t a place that can be changed. The fix depends on what that target is. Most cases come down to a few patterns: a literal, a calculation, a const variable, a whole array, a function that returns a copy, or a character inside a string literal.
What “modifiable lvalue” means
In C++, an lvalue is an expression that refers to an object you can name and find in memory, such as a variable. A modifiable lvalue is one you’re allowed to change, which rules out anything declared const. An assignment needs a modifiable lvalue on its left side, because the new value has to be stored somewhere.
Literals like 5 and calculations like a + b are rvalues. They produce values rather than places, so there’s nowhere to store anything. If the term is new to you, What Does lvalue Mean? explains the idea in more depth.
All of the assignment operators, such as = and +=, need a modifiable lvalue on the left. Assignment Operators in C++ covers the full set.
Read the line the compiler points to
Most compilers report the line where the problem is, and many also give a column. Look at the left side of the = on that line, or at the operand of ++ or --. Then ask:
- Is it a variable name, or something else, like a number or a calculation?
- If it’s a variable, was it declared
const? - If it’s a function call, does the function return a reference or a copy?
- Is it an array, or a character inside a string literal?
Your answer points to one of the cases below.
Common causes and fixes
A literal on the left
int count = 0;
5 = count; // error: 5 is a value, not a place
count = 5; // fine
This usually happens when the two sides of an assignment get swapped. The thing being changed always goes on the left.
A calculation on the left
int sum = 0;
int total = 10;
sum + 1 = total; // error: sum + 1 is a calculated value
sum = total + 1; // fine
A calculation produces a value, so there’s nothing to assign to. Put the variable you want to change on the left.
A const variable
const int max_users = 100;
max_users = 200; // error: a const object can't be changed
A const object is an lvalue, but it isn’t modifiable. If the value should change, remove const from the declaration. If it shouldn’t, copy the value into a new variable and change that instead:
int users = max_users; // users is a copy that can change
users = 200;
The const Keyword in C++ explains when const is worth using. Some compilers word this error differently, such as “assignment of read-only variable”, but the cause is the same.
A whole array
int a[3] = {1, 2, 3};
int b[3] = {4, 5, 6};
a = b; // error: a C-style array can't be assigned as a whole
To copy one array into another, copy the elements with std::copy:
#include <algorithm>
#include <iterator>
std::copy(std::begin(b), std::end(b), std::begin(a)); // a now holds 4, 5, 6
If you have the choice, std::array is easier, because it can be assigned like any other value:
#include <array>
std::array<int, 3> c = {1, 2, 3};
std::array<int, 3> d = {4, 5, 6};
c = d; // fine: c now holds 4, 5, 6
A function that returns a copy
int count = 0;
int get_count() {
return count; // returns a copy of the value
}
get_count() = 4; // error: the result is a temporary value
The fix depends on what you meant. If you only needed the number, store it first:
int current = get_count();
current = 4; // changes the copy, not count
If the call should refer to the original variable, return a reference instead:
int& get_count() {
return count; // returns a reference to count
}
get_count() = 4; // fine: count is now 4
This works because count is a global variable, so it still exists after the function returns. Never return a reference to a local variable, because that variable is destroyed when the function ends.
A conditional expression with a literal branch
int x = 1;
bool flag = true;
(flag ? x : 0) = 5; // error: 0 is a value, so the whole expression is a value
The conditional operator produces something you can assign to only when both branches are places. (flag ? x : y) = 5; compiles when x and y are both int variables. If you meant to change x only when flag is true, use an if statement:
if (flag) {
x = 5;
}
The Conditional Operator in C++ covers how ?: chooses its result.
A character inside a string literal
"hello"[0] = 'H'; // error: characters in a string literal are read-only
To change a character, copy the text into something writable first:
char word[] = "hello"; // a writable array that holds a copy of the text
word[0] = 'H';
std::string name = "hello";
name[0] = 'H';
Why string literals are read-only is covered in What Does char* Do in C++?
Using ++ or – on a value
int a = 2;
int b = 3;
++(a + b); // error: there's no variable to increase
The increment and decrement operators need a modifiable lvalue too, so (a + b)++ fails for the same reason. The Increment Operator in C++ explains how they work with variables.
Class types follow a different rule
The rules above are strictest for built-in types such as int, char and pointers. Assignment to a class type, such as std::string, is a call to a member function. A member function can run on a temporary object, so an assignment like get_name() = "Ada"; can compile even when get_name returns a std::string by value. That inconsistency is confusing, so don’t use it as a pattern. Assign to a named variable instead.
Compiler messages are worded differently
The wording depends on the compiler and its version, so search for the idea rather than an exact phrase. A few examples:
- GCC:
lvalue required as left operand of assignment - Clang:
expression is not assignable - Microsoft Visual C++:
left operand must be l-value
Whatever the wording, the checks above still apply. Look at the left side of the highlighted line first.
How to avoid it
- Put the variable you want to change on the left:
total = price + tax; - Only mark a value
constif it should never change. - Don’t assign to whole C-style arrays. Use
std::array, or copy the elements. - If a function should return something you can assign to, have it return a reference, as long as the object it refers to outlives the call.
- When the compiler points to a line, check the left side of
=,++and--first.
Closing
Most of the time, this error means the code is trying to store a value in something that isn’t a place: a literal, a calculation, a constant, a copy returned from a function, or a character of a string literal. Once you know which of those you have, the fix is usually a small change.