C++
C++ Expected Primary-Expression Fix
Somewhere in the expression there is a hole - a place where a value had to start and something else is standing instead. The compiler names the token that follows the hole, which makes the diagnostic a map: find the named token, and the missing expression belongs immediately before it. This guide separates the five places this error most often hides, reads what each wording of the diagnostic actually reports, and matches each shape to the value, condition, or index that fills it.
Written November 24, 2026 · Tested with GCC 13.3.0 · 9 min read
- The rule underneath: an operator leaves the parser waiting for an expression
- How to read the diagnostic
- The first failure: missing operand after a binary operator
- The second failure: trailing comma in a function call
Written November 24, 2026
Tested with GCC 13.3.0
What this guide is
An expression is built from values and the operators that combine them, and every operator expects a value on each side it works on. When one of those values is missing - deleted in an edit, never typed, or separated from its operator by a stray comma - the parser reaches the next token still waiting for an expression to begin, and that token is the one the error names. The fix is to supply what is missing, but finding the hole means reading the diagnostic at face value: the named token is where the parser stopped, and the missing expression belongs immediately before it.
The five forms in this guide each reproduce the failure with a small, complete program, and the hole is in five different places: after a binary operator whose right operand is missing, after a trailing comma in a function call, inside an empty if condition, inside an empty subscript, and between two binary operators written in a row. In each family the failing program is small, the full compiler output is shown, and the fix fills the gap - an operand, a condition, or an index - or removes the punctuation that created it. Four neighboring shapes in the traps are not this error at all: omitting the middle operand of a conditional compiles as a GNU extension, a unary operator legitimately starts an expression, empty parentheses on a call to a zero-parameter function and a bare return; in a void function are complete as written, and empty braces as an initializer value-initialize instead of expressing nothing.
One neighboring failure is easy to confuse with this one and is not covered here. If a value is present but has the wrong form - a name that was never declared, or an argument of the wrong type - the compiler fails with errors such as "was not declared in this scope" or a conversion error, which have their own guides. This guide's error is about absence, not mismatch: the diagnostic says a primary-expression was expected, and there is simply nothing where one had to be.
Build every program in this guide with g++ -std=c++20 -Wall -Wextra. The failing programs are compiled with -c - compile only - because the error prevents any program from being produced; the fixed programs and the traps that compile are compiled and run, so their output is shown.
The rule underneath: an operator leaves the parser waiting for an expression
The smallest unit an expression is built from is a primary expression - a literal, a name, a parenthesized expression, and a handful of other self-contained forms. Everything larger is assembled from primary expressions: a binary operator takes an expression on its left and expects one to begin on its right, a subscript's brackets enclose an expression that gives the index, and a function call's parentheses enclose the argument expressions, separated by commas. The expressions grammar is why the error is worded the way it is: wherever the parser has committed to reading an expression and the next token cannot start one, the token it found is reported as standing where a primary-expression was expected.
That single rule produces all five families. A binary operator with nothing after it never gets its right operand, so the statement's semicolon is named. A trailing comma in a call promises another argument expression, so the closing parenthesis is named. An if condition is an expression, so empty parentheses name their own closing parenthesis. A subscript with nothing between its brackets has no index expression at all, so the closing bracket is named. And a second binary operator cannot start an expression - only a value, or a unary operator, can - so the second operator itself is named.
Three consequences explain every family below. First, the named token is innocent: it is the token that closed the gap, and the repair goes immediately before it, not on it. Second, each family below produces exactly one error, because each program has exactly one hole - a real program with this error often prints a short cascade after the first failure, and the cascade is read the same way: fix the first hole, recompile, and only then read what remains. Third, a gap is only a gap where the grammar requires an expression - the traps show the places that look empty but are not expressions at all, and the one place, the conditional's middle operand, where this compiler accepts an omission the standard does not.
How to read the diagnostic
Every failure in the five families prints the same shape of output. The second family's, in full:
fail_trailing_comma_call.cpp: In function ‘int main()’:
fail_trailing_comma_call.cpp:8:13: error: expected primary-expression before ‘)’ token
8 | show(1, );
| ^
Read it in order. The first line gives the file and the position - here the closing parenthesis of the call - and then the error itself: a primary-expression was expected before that token. The caret sits under the named token, not under the place the expression is missing from - in this family the two are adjacent, the comma on one side and the parenthesis on the other, and in every family the missing expression belongs on the caret's left, where the operator, comma, bracket, or condition was left waiting.
Three practical consequences follow. First, work leftwards: the reported token - a semicolon, a closing parenthesis, a closing bracket, a second operator - is where the parser gave up; the hole is the space just before it. Second, count the errors before trusting the first fix: each family below produces exactly one error from its one hole, so a transcript with several errors usually means several holes - or one hole derailing what follows it - and either way the first reported hole is the one to fill first. Third, the wording never varies inside this guide's error: all five families say "expected primary-expression before" a token, and the token is the whole clue - name the construct that token closes, and that construct is the one with the gap.
The first failure: missing operand after a binary operator
A program that starts an addition and never supplies its right operand:
#include <iostream>
int main() {
int total = 5 + ;
std::cout << total << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_missing_operand.o fail_missing_operand.cpp
fail_missing_operand.cpp: In function ‘int main()’:
fail_missing_operand.cpp:4:21: error: expected primary-expression before ‘;’ token
4 | int total = 5 + ;
| ^
The error is reported at the semicolon - the first token after the operator that cannot start an expression. The + is a binary operator: with 5 on its left, the parser is committed to reading an expression on its right, and the semicolon ends the statement instead. This is the most common form of the error in real code, usually left behind when the right side of an expression is deleted or moved and its operator is left standing.
The fix supplies the missing operand:
#include <iostream>
int main() {
int total = 5 + 3;
std::cout << total << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_missing_operand fix_missing_operand.cpp
$ ./fix_missing_operand
8
The output is 8. One operand added after the operator; the semicolon the error named was correct all along. Deleting the operator instead would also close the gap - the choice is whether the operation was meant to happen, and only the program's intent answers that.
The second failure: trailing comma in a function call
A program that calls a one-argument function with a comma after its only argument:
#include <iostream>
void show(int value) {
std::cout << value << '\n';
}
int main() {
show(1, );
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_trailing_comma_call.o fail_trailing_comma_call.cpp
fail_trailing_comma_call.cpp: In function ‘int main()’:
fail_trailing_comma_call.cpp:8:13: error: expected primary-expression before ‘)’ token
8 | show(1, );
| ^
The wording names the closing parenthesis. Inside a call, a comma is a promise: it separates one argument expression from the next one, so after the comma the parser waits for another argument to begin - and the closing parenthesis arrives instead. Unlike the commas in the first family's cousin errors elsewhere, this comma is not extra punctuation the parser can ignore; in an argument list a comma always has a right side coming. Trailing commas are legal in some other comma lists in C++ - an initializer list, for example - which is why this shape survives editing habits formed there.
The fix removes the comma, since the function takes one argument:
#include <iostream>
void show(int value) {
std::cout << value << '\n';
}
int main() {
show(1);
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_trailing_comma_call fix_trailing_comma_call.cpp
$ ./fix_trailing_comma_call
1
The output is 1. One character removed; had the function taken a second argument, the same error would instead be fixed by writing that argument where the comma promised it.
The third failure: empty if condition
A program whose if has parentheses with nothing between them:
#include <iostream>
int main() {
int x = 1;
if () {
std::cout << x << '\n';
}
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_empty_if_condition.o fail_empty_if_condition.cpp
fail_empty_if_condition.cpp: In function ‘int main()’:
fail_empty_if_condition.cpp:5:9: error: expected primary-expression before ‘)’ token
5 | if () {
| ^
The wording again names the closing parenthesis - the same token as the second family, in a different construct. An if condition is an expression: the statement cannot be read until a condition begins, and here the parenthesis that should close the condition arrives before any condition has started. The shape usually comes from a condition deleted during editing, or from a placeholder written while the surrounding code was drafted and never filled in.
The fix writes the condition:
#include <iostream>
int main() {
int x = 1;
if (x == 1) {
std::cout << x << '\n';
}
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_empty_if_condition fix_empty_if_condition.cpp
$ ./fix_empty_if_condition
1
The output is 1, printed because the condition holds. The parentheses were never the problem - they were waiting for the expression they exist to enclose.
The fourth failure: empty subscript in an expression
A program that subscripts an array with nothing between the brackets:
#include <iostream>
int main() {
int values[3] = {10, 20, 30};
int total = values[];
std::cout << total << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_empty_subscript.o fail_empty_subscript.cpp
fail_empty_subscript.cpp: In function ‘int main()’:
fail_empty_subscript.cpp:5:24: error: expected primary-expression before ‘]’ token
5 | int total = values[];
| ^
The wording names the closing bracket. A subscript is an expression in brackets - the index - so the brackets here enclose a hole: the parser waits for the index expression to begin and the closing bracket arrives instead. The contrast that makes this shape easy to misread is the declaration a few lines above it: in int values[3], the brackets carry a size, and an array declaration may even omit that size when an initializer supplies it - but in an expression, brackets after a value are always a subscript, and a subscript always needs its index.
The fix supplies the index:
#include <iostream>
int main() {
int values[3] = {10, 20, 30};
int total = values[1];
std::cout << total << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_empty_subscript fix_empty_subscript.cpp
$ ./fix_empty_subscript
20
The output is 20, the element at index 1. One expression added between the brackets; the bracket the error named was, again, only the token that closed the gap.
The fifth failure: two binary operators in a row
A program whose expression places a division operator immediately after a multiplication operator:
#include <iostream>
int main() {
int x = 5 * / 2;
std::cout << x << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_double_operator.o fail_double_operator.cpp
fail_double_operator.cpp: In function ‘int main()’:
fail_double_operator.cpp:4:17: error: expected primary-expression before ‘/’ token
4 | int x = 5 * / 2;
| ^
The wording names the second operator itself. After *, the parser waits for the expression the multiplication works on, and / cannot start one - a binary operator combines expressions, it is not one, and what can start the missing expression is a value, a parenthesized expression, a cast, or a unary operator, never another binary operator. This family is the one where the named token is itself part of the mistake, rather than an innocent token after it: one of the two operators is stray, most often the survivor of an edit that changed an expression's operation and left both spellings in place. The traps draw the line precisely - a unary operator in the same position would have been legal, because a unary operator does start an expression.
The fix keeps the one operator the computation intends - here division:
#include <iostream>
int main() {
int x = 5 / 2;
std::cout << x << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_double_operator fix_double_operator.cpp
$ ./fix_double_operator
2
The output is 2, the integer quotient of 5 divided by 2. One operator removed; if the multiplication was the intended one, removing the / instead gives the same shape of repair, and the program's intent decides which.
The fixes in reach order
- The error names a token: look immediately to its left - that is where the missing expression belongs.
- Before a semicolon after an operator: supply the operator's missing operand, or remove the operator if the operation was not intended.
- Before a closing parenthesis after a comma in a call: remove the trailing comma, or write the next argument the comma promised.
- Before a closing parenthesis in an
if: write the condition - the parentheses enclose an expression, never nothing. - Before a closing bracket: write the subscript's index expression.
- Before a second binary operator: remove one of the two operators - or, if a sign or dereference was intended, write it as the unary operator it is.
- In every case: fix the first reported hole, recompile, and only then read any errors that remain.
Traps
- An omitted middle operand of the conditional operator compiles here - as a GNU extension, with a warning, not as this error. Here the operand between
?and:is left out:
#include <iostream>
int main() {
int x = 1;
int y = x > 0 ? : 2;
std::cout << y << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_ternary_extension trap_ternary_extension.cpp
trap_ternary_extension.cpp: In function ‘int main()’:
trap_ternary_extension.cpp:5:21: warning: the omitted middle operand in ‘?:’ will always be ‘true’, suggest explicit middle operand [-Wparentheses]
5 | int y = x > 0 ? : 2;
| ^
$ ./trap_ternary_extension
1
1. The program compiles and runs: under the extension, the condition is evaluated once and used as its own middle operand, so y takes the condition's value when it holds. The warning is the whole story - it appears under -Wall and names the omission directly. This is the one gap in this guide that is not an error on this compiler, and it is not standard C++: code that must build everywhere writes the middle operand out.
- A unary operator starts a valid expression - it is not a second binary operator. Here four unary operators each begin the expression on their operator's right:#include <iostream>
int main() {
int value = 5;
bool flag = false;
int* ptr = &value;
std::cout << -value << '\n';
std::cout << !flag << '\n';
std::cout << *ptr << '\n';
std::cout << *(&value) << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_unary_operators trap_unary_operators.cpp
$ ./trap_unary_operators
-5
1
5
5
The output is -5, 1, 5, and 5, one per line. Negation, logical not, dereference, and address-of are unary: each takes the expression that follows it as its operand, so each can stand exactly where the fifth family's second binary operator could not - at the start of an expression, including immediately after a binary operator, as in 5 * -2. The line between the fifth family and this trap is whether the operator in the gap can begin an expression, and unary operators can.
- Empty parentheses on a call are an argument list, not an empty expression. Here a function that takes no parameters is called with empty parentheses:
#include <iostream>
void greet() {
std::cout << "ready" << '\n';
}
int main() {
greet();
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_empty_call trap_empty_call.cpp
$ ./trap_empty_call
ready
The output is ready. Nothing is missing between the parentheses: a call to a zero-parameter function has no argument expressions to write, so the empty pair is complete - the direct contrast with the third family, where the if condition is an expression and its empty parentheses are a hole.
- A bare return; in a void function is complete for the same reason. Here a void function returns with no value:
#include <iostream>
void show() {
std::cout << "done" << '\n';
return;
}
int main() {
show();
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_void_return trap_void_return.cpp
$ ./trap_void_return
done
The output is done. A void function returns no value, so there is no return expression to be missing - the semicolon after return ends a complete statement. A non-void function is the opposite case: there the return value is a required expression, and leaving it out is a different error, not this one.
- Empty braces as an initializer value-initialize - they are not an empty expression. Here an integer is initialized with empty braces:
#include <iostream>
int main() {
int x{};
std::cout << x << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_empty_braces_init trap_empty_braces_init.cpp
$ ./trap_empty_braces_init
0
The output is 0. The braces are an initializer, not an expression waiting for a value: empty-brace initialization value-initializes the object, which for an int means zero. The empty parentheses and brackets of the third and fourth families, sitting where an expression is required, are holes; empty braces are an initializer wherever they appear - as a declaration initializer, a function argument, or the right side of an assignment - and as an initializer they are a complete instruction with a defined result.
Checklist
- The reported token treated as the parser's stopping point - the hole is the space immediately before it, and the repair goes there.
- After a binary operator: its right operand present, or the operator removed.
- In a function call: no comma left promising an argument that never arrives - every comma followed by an argument expression.
- An
ifcondition: a real expression between the parentheses. - A subscript in an expression: an index between the brackets - brackets in a declaration, where a size may be omitted, are a different construct.
- Two operators in a row checked for which one is stray - and a unary operator in that position left alone, because it starts the expression legally.
- An omitted conditional operand recognized as an extension, not relied on: the middle operand written out in code that must build everywhere.
- Empty parentheses on a zero-parameter call, a bare
return;in a void function, and empty braces as an initializer left alone - none of them is an empty expression. - The fixed program run, and its output checked against what the program was meant to do.
Related guides on this site
- 'std::expected' Is Not a Member of 'std' Fix
- C++ Expected Unqualified-Id Fix
- C++ Expected Semicolon Error Fix
- Return errors as values with std::expected
Sources
- cppreference, Expressions - for primary expressions being the building blocks expressions are assembled from, and for how operators combine the expressions on either side of them.
- cppreference, Member access operators - for the built-in subscript operator taking an index expression between its brackets, and for a function call's argument expressions being separated by commas.
- cppreference, if statement - for the if statement's condition, in the form used here an expression enclosed in parentheses (a condition can also be a single declaration).
- cppreference, Other operators - for the conditional operator's three operands, the middle one being the expression the GNU extension allows to be omitted.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.