C++
C++ Expected Semicolon Error Fix
The semicolon is missing on one line and the compiler complains about the next one - because a declaration is not finished until its semicolon arrives, so the parser keeps reading into whatever follows and reports the failure there. 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 its one-character repair.
Written November 24, 2026 · Tested with GCC 13.3.0 · 8 min read
- The rule underneath: a declaration is not finished until its semicolon
- How to read the diagnostic
- The first failure: missing semicolon after a variable declaration
- The second failure: missing semicolon after a class definition
Written November 24, 2026
Tested with GCC 13.3.0
What this guide is
A missing semicolon rarely produces an error on the line that is missing it. A declaration ends at its semicolon, so without one the parser treats the next line as a continuation of the same declaration, and the error is reported where that continuation stops making sense - at the next token. The fix is one character, but finding where it belongs means reading the diagnostic backwards: the reported line is where the parser gave up, and the missing semicolon belongs at the end of the construct just before it.
The five forms in this guide each reproduce the failure with a small, complete program, and the semicolon is missing in five different places: after a variable declaration, after a class definition's closing brace, after a member declaration inside a class, after a return statement, and in a for-loop header, where commas were written where the header needs semicolons. In each family the failing program is small, the full compiler output is shown, and the fix adds the one semicolon - or, in the loop header, replaces the commas with the semicolons the header's grammar requires. Four neighboring shapes in the traps are not this error at all: an extra semicolon after a function body is legal, a stray semicolon can serve as an if statement's whole body, an extra semicolon after a namespace definition is legal, and a semicolon after a member function definition inside a class is optional. One further trap is this error in a sixth place - after a do-while loop, which is one of the few statements that ends with a semicolon after its closing brace.
One neighboring failure is easy to confuse with this one and is not covered here. If a name itself is wrong - misspelled, undeclared, or of the wrong kind - the compiler fails with errors such as "was not declared in this scope" or "does not name a type", which have their own guides. This guide's error is about punctuation, not names: the diagnostic says a semicolon was expected, and every name in the program may be perfectly correct.
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: a declaration is not finished until its semicolon
A simple declaration - the form that declares variables - is written as a sequence of specifiers, a list of declarators, and a terminating semicolon: the semicolon is part of the declaration's syntax, not a separator placed between declarations. Until it arrives, the declaration has not ended, which is why the token that follows is read as more of the same declaration and why the error lands there.
The same shape repeats inside a class. In a class's member specification, a member declaration is written with its declarators followed by a semicolon, and a class definition itself appears inside a declaration - which is why a defined class that will not be followed by declarators still needs a semicolon after its closing brace: that semicolon is the enclosing declaration's terminator. A member function defined inside the class body is the stated exception: a semicolon after a member function definition is optional.
The for loop carries the same punctuation in its header. A for statement is written for ( init-statement condition ; expression ) - the init-statement must end with a semicolon, and a second semicolon separates the condition from the expression. Commas do different work there: inside a declaration, a comma separates declarators in one list, so a comma in the header extends the current clause instead of ending it.
Three consequences explain every family below. First, the error position is the parser's stopping point, not the omission point: look one construct backwards from the reported token. Second, the wording varies with the construct - "before" a token, "after class definition", "at end of member declaration" - and each wording names the construct whose terminator is missing, which makes the wording itself the best clue. Third, an extra semicolon is usually harmless where a missing one is fatal, because at namespace and class scope a standalone semicolon is an empty declaration with no effect - the traps show where that holds and the one common shape, the stray semicolon after an if condition, where it changes what the program does while still compiling.
How to read the diagnostic
Every failure in the five families prints the same shape of output. The second family's, in full:
fail_missing_semicolon_class.cpp:6:2: error: expected ‘;’ after class definition
6 | }
| ^
| ;
Read it in order. The first line gives the file and the position - here the closing brace of the class definition - and then the error itself: a semicolon was expected after the class definition. GCC adds a further line that most of the other families also show: a lone semicolon under the position, suggesting the exact character and the exact place to insert it. That suggestion line is the fastest read of this whole error family - it is the compiler pointing at the end of the construct that needed terminating.
Three practical consequences follow. First, work backwards: the reported token - std, a closing brace, the next member - is innocent; the line or construct immediately before it is where the semicolon belongs. Second, count the errors before trusting the first fix: four of the five families below produce exactly one error, but the for-loop family produces three from the same two missing semicolons, because each comma derails the header's grammar at a different point. Fix the punctuation and recompile rather than chasing each error separately. Third, a warning can ride along with the error without being a second problem: the first family's transcript also warns that the variable is unused, which is a side effect of the failed parse - the statement that used the variable was consumed as part of the broken declaration - and the warning disappears with the fix.
The first failure: missing semicolon after a variable declaration
A program that declares a variable and then uses it, with no semicolon ending the declaration:
#include <iostream>
int main() {
int total = 5
std::cout << total << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_missing_semicolon_variable.o fail_missing_semicolon_variable.cpp
fail_missing_semicolon_variable.cpp: In function ‘int main()’:
fail_missing_semicolon_variable.cpp:5:5: error: expected ‘,’ or ‘;’ before ‘std’
5 | std::cout << total << '\n';
| ^~~
fail_missing_semicolon_variable.cpp:4:9: warning: unused variable ‘total’ [-Wunused-variable]
4 | int total = 5
| ^~~~~
The error is reported on line 5, at std - the first token of the next statement - and it offers two ways the declaration could have ended: "expected ',' or ';' before 'std'". The comma alternative is real grammar: a comma would have made std part of the same declarator list, declaring more variables. Neither was written, so the declaration never ended and the parser stopped at the first token it could not absorb. This is the most common form of the error, and the one whose reported line is most misleading: line 5 is correct in every way, and editing it changes nothing.
The fix ends the declaration:
#include <iostream>
int main() {
int total = 5;
std::cout << total << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_missing_semicolon_variable fix_missing_semicolon_variable.cpp
$ ./fix_missing_semicolon_variable
5
The output is 5. One character added at the end of line 4; the reported line was never touched. The unused-variable warning from the failing transcript is gone too - with the declaration properly terminated, the next statement is read as a statement again and the variable is used.
The second failure: missing semicolon after a class definition
A program that defines a class and then uses it, with no semicolon after the class's closing brace:
#include <iostream>
class Counter {
public:
int value = 7;
}
int main() {
Counter c;
std::cout << c.value << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_missing_semicolon_class.o fail_missing_semicolon_class.cpp
fail_missing_semicolon_class.cpp:6:2: error: expected ‘;’ after class definition
6 | }
| ^
| ;
The wording is the most direct of the five: "expected ';' after class definition", reported at the closing brace itself, with the suggested semicolon printed below it. The reason a class definition needs the terminator is that the definition sits inside a declaration - the same declaration form that can go on to declare objects, as in a definition followed by variable names before the semicolon. With no declarators following, the semicolon still has to close the declaration. This form appears most often after a class or struct is written by analogy with a function definition, whose closing brace needs no semicolon - the traps return to that contrast.
The fix adds the semicolon after the brace:
#include <iostream>
class Counter {
public:
int value = 7;
};
int main() {
Counter c;
std::cout << c.value << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_missing_semicolon_class fix_missing_semicolon_class.cpp
$ ./fix_missing_semicolon_class
7
The output is 7, the member's initializer value. One character, placed exactly where the diagnostic's suggestion line put it.
The third failure: missing semicolon after a member declaration
A program whose struct declares two members, with no semicolon ending the first member's declaration:
#include <iostream>
struct Point {
int x
int y = 3;
};
int main() {
Point p{4, 3};
std::cout << p.x << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_missing_semicolon_member.o fail_missing_semicolon_member.cpp
fail_missing_semicolon_member.cpp:4:9: error: expected ‘;’ at end of member declaration
4 | int x
| ^
| ;
The wording names the place precisely: "expected ';' at end of member declaration", reported at the end of the first member's line, with the suggested semicolon below it. Inside a class body, each member declaration is terminated by its own semicolon - the member specification is a sequence of such declarations - so a missing one merges the next member into the current declaration, and the parser stops at the end of the first declarator, where the terminator had to be. Of the five families, this one points closest to the omission itself.
The fix ends the member declaration:
#include <iostream>
struct Point {
int x;
int y = 3;
};
int main() {
Point p{4, 3};
std::cout << p.x << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_missing_semicolon_member fix_missing_semicolon_member.cpp
$ ./fix_missing_semicolon_member
4
The output is 4, the value the aggregate initialization gave the first member. One character added inside the class body; nothing outside it changed.
The fourth failure: missing semicolon after a return statement
A function that computes a value and returns it, with no semicolon ending the return statement:
#include <iostream>
int answer() {
int total = 42;
return total
}
int main() {
std::cout << answer() << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_missing_semicolon_return.o fail_missing_semicolon_return.cpp
fail_missing_semicolon_return.cpp: In function ‘int answer()’:
fail_missing_semicolon_return.cpp:5:17: error: expected ‘;’ before ‘}’ token
5 | return total
| ^
| ;
6 | }
| ~
The wording is "expected ';' before '}' token": a return statement ends with a semicolon, just as a declaration does, so without one the parser read on into the function's closing brace and stopped there. The transcript underlines both places - the suggested semicolon at the end of the return line, and the brace on the next line where the parser gave up. A return statement is easy to leave unterminated because the brace below it looks like an ending; grammatically it is the semicolon that ends the statement, and the brace only ends the function.
The fix ends the return statement:
#include <iostream>
int answer() {
int total = 42;
return total;
}
int main() {
std::cout << answer() << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_missing_semicolon_return fix_missing_semicolon_return.cpp
$ ./fix_missing_semicolon_return
42
The output is 42. One character added; the closing brace was correct all along.
The fifth failure: commas instead of semicolons in a for-loop header
A loop whose header separates its three clauses with commas instead of semicolons:
#include <iostream>
int main() {
for (int i = 0, i < 3, ++i) {
std::cout << i << '\n';
}
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_for_loop_commas.o fail_for_loop_commas.cpp
fail_for_loop_commas.cpp: In function ‘int main()’:
fail_for_loop_commas.cpp:4:22: error: expected ‘;’ before ‘<’ token
4 | for (int i = 0, i < 3, ++i) {
| ^~
| ;
fail_for_loop_commas.cpp:4:23: error: expected primary-expression before ‘<’ token
4 | for (int i = 0, i < 3, ++i) {
| ^
fail_for_loop_commas.cpp:4:31: error: expected ‘;’ before ‘)’ token
4 | for (int i = 0, i < 3, ++i) {
| ^
| ;
This family produces three errors, and all three come from the same two commas. The first comma is grammatically valid - in the init-statement's declaration it separates declarators, so i < 3 is first read as more declarators - and the parser stops at the < with "expected ';' before '<' token": the init-statement had to end with a semicolon before the condition could begin. The second error, at the same <, is the same derailment read as an expression problem, and the third asks for a semicolon before the closing parenthesis, where the second comma left the header's last clause unterminated. The lesson of the three errors is that a for header's clauses are separated by semicolons and nothing else: a comma never ends a clause, it extends one.
The fix writes the header with semicolons:
#include <iostream>
int main() {
for (int i = 0; i < 3; ++i) {
std::cout << i << '\n';
}
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_for_loop_commas fix_for_loop_commas.cpp
$ ./fix_for_loop_commas
0
1
2
The output is 0, 1, and 2, one per line. Two characters changed - both commas became semicolons - and all three errors leave together, because they were never three problems.
The fixes in reach order
- The error names a token on the following line: look at the end of the line or construct just before it - that is where the semicolon belongs.
- "After class definition": add the semicolon after the class or struct definition's closing brace.
- "At end of member declaration": add the semicolon at the end of that member's declaration, inside the class body.
- "Before '}' token" after a return or an expression: end that statement with a semicolon; the brace only closes the block.
- Inside a for-loop header: the clauses are separated by semicolons - replace any comma that was meant to end a clause, and expect several errors from the same header to clear together.
- In every case: fix the first reported construct, recompile, and only then read any errors that remain.
Traps
- An extra semicolon after a function body is legal. Here a semicolon follows the definition of
answer:
#include <iostream>
int answer() {
return 42;
};
int main() {
std::cout << answer() << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o probe_extra_semicolon_function probe_extra_semicolon_function.cpp
$ ./probe_extra_semicolon_function
42
42. A function definition needs no terminating semicolon, and the extra one is read as an empty declaration, which has no effect. This is the direct contrast with the second family: the same character after a class definition's brace is required, and after a function definition's brace is merely harmless.
- A stray semicolon can be an if statement's whole body - and that is a change of meaning, not an error. Here the semicolon after the condition is the if's body, and the output statement below it is a separate, unconditional statement:#include <iostream>
int main() {
int x = 1;
if (x == 1);
std::cout << "ready" << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o probe_stray_semicolon_if probe_stray_semicolon_if.cpp
probe_stray_semicolon_if.cpp: In function ‘int main()’:
probe_stray_semicolon_if.cpp:5:16: warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]
5 | if (x == 1);
| ^
probe_stray_semicolon_if.cpp:5:5: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
5 | if (x == 1);
| ^~
probe_stray_semicolon_if.cpp:6:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
6 | std::cout << "ready" << '\n';
| ^~~
$ ./probe_stray_semicolon_if
ready
The output is ready, and the program compiles - with two warnings, not errors: one suggesting braces around the empty body, and one warning that the following statement is misleadingly indented as if the if guarded it. It does not: the indented statement runs whatever the condition holds. A missing semicolon is loud; this extra one is quiet, and the warnings are the only sign - which is why they are worth reading instead of dismissing.
- An extra semicolon after a namespace definition is legal too. Here a semicolon follows the namespace's closing brace:
#include <iostream>
namespace values {
int answer = 42;
};
int main() {
std::cout << values::answer << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o probe_extra_semicolon_namespace probe_extra_semicolon_namespace.cpp
$ ./probe_extra_semicolon_namespace
42
The output is 42. A namespace definition, like a function definition, needs no terminating semicolon, and the extra one is again an empty declaration. Of the three brace-ending constructs in this guide, only the class definition requires the semicolon - because only it sits inside a declaration that the semicolon terminates.
- A missing semicolon after a do-while loop is this error again, in the one statement that imitates a class. A do-while ends with while (condition); - the semicolon after the closing parenthesis is part of the statement:
$ g++ -std=c++20 -Wall -Wextra -c -o probe_missing_semicolon_do_while.o probe_missing_semicolon_do_while.cpp
probe_missing_semicolon_do_while.cpp: In function ‘int main()’:
probe_missing_semicolon_do_while.cpp:7:20: error: expected ‘;’ before ‘std’
7 | } while (i < 3)
| ^
| ;
8 | std::cout << i << '\n';
| ~~~
The failing program:
#include <iostream>
int main() {
int i = 0;
do {
++i;
} while (i < 3)
std::cout << i << '\n';
return 0;
}
The wording is "expected ';' before 'std'" - the same shape as the first family, reported at the next statement. The do-while is the mirror image of the function definition: its brace is followed first by the rest of the statement and then by a required semicolon, so it is the one loop whose ending is easy to leave unterminated by analogy with the loops that need nothing after their bodies. - A semicolon after a member function definition is optional. Here one follows a member function defined inside its struct:
#include <iostream>
struct Greeter {
int value() {
return 42;
};
};
int main() {
Greeter g;
std::cout << g.value() << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o probe_semicolon_after_member_function probe_semicolon_after_member_function.cpp
$ ./probe_semicolon_after_member_function
42
The output is 42. Inside a class body, member declarations end with semicolons, but a member function definition is the exception the class rules state: the semicolon after it may be written or omitted, and neither form changes the program.
Checklist
- The reported token treated as the parser's stopping point, not the error's location - the construct immediately before it checked for its terminator.
- The wording matched to its construct: after a class definition, at the end of a member declaration, before a following token, or before a closing brace.
- A class or struct definition's closing brace followed by a semicolon.
- Every member declaration inside a class body ended with its own semicolon.
- Return and expression statements ended with semicolons, not left to the closing brace below them.
- A for-loop header read clause by clause: init-statement ended by a semicolon, condition followed by a semicolon, commas only inside a clause.
- A do-while loop's closing
while (...)followed by its semicolon. - Extra semicolons left alone where they are legal empty declarations - and the one after an
ifcondition checked, because there it changes the meaning. - 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
- R_X86_64_PC32 Relocation Error Fix
- R_X86_64_32 Relocation Error Fix
- C++ Expected Unqualified-Id Fix
Sources
- cppreference, Declarations - for a simple declaration being written as specifiers and a declarator list terminated by a semicolon, for a declarator list being comma-separated, and for a standalone semicolon being an empty declaration with no effect.
- cppreference, Class declaration - for member declarations in a class's member specification being written with a terminating semicolon, and for a semicolon after a member function definition being optional.
- cppreference, for loop - for the for statement's syntax (init-statement, condition, a semicolon, expression), and for any init-statement having to end with a semicolon.
- cppreference, do-while loop - for the do-while statement's syntax, which ends
while (expression);with the semicolon included.
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.