How to Fix “expected ’;’” Errors in C++
Short answer: the compiler reached a point where a statement should already have ended, and there was no semicolon. Look at the end of the line above the error first — that’s where the ; is missing most of the time. In Visual Studio the same error reads expected a ';', and if no semicolon is missing there, you’re probably missing a closing }.
Every compiler words it a little differently:
| Compiler | What you see |
|---|---|
| GCC (g++, MinGW, Code::Blocks, Dev-C++) | expected ';' before 'return' |
| Clang (Xcode, macOS) | expected ';' after expression |
| Visual Studio (red squiggle) | expected a ';' |
| Visual Studio (when you build) | C2143: syntax error: missing ';' before 'return' |
They all mean the same thing. Below are the eight usual causes, the exact message each one produces, and the fix.
Why the Error Points at the Wrong Line
C++ ignores line breaks. To the compiler, your program is one long stream of words and symbols, and the semicolon is the only thing that marks where one statement ends and the next begins.
So when you forget a ; at the end of a line, the compiler doesn’t notice straight away. It keeps reading onto the next line, and only complains when it hits something that can’t possibly continue the statement — usually the first word of the next line.
Newer versions of GCC and Clang are good at pointing back to the exact spot. Visual Studio usually reports the error at the next token, which is why its errors so often look one line too low. Microsoft’s own documentation advises checking the lines above the error.
Cause 1: A Missing Semicolon at the End of a Statement
The classic. One line is missing its ;:
#include <iostream>
int main() {
std::cout << "Hello"
return 0;
}
GCC: main.cpp:4:25: error: expected ';' before 'return'
Clang: main.cpp:4:25: error: expected ';' after expression
MSVC: error C2143: syntax error: missing ';' before 'return'
Read GCC’s message literally: the ; belongs before return, which means at the end of the line above it.
Fix:
#include <iostream>
int main() {
std::cout << "Hello";
return 0;
}
Cause 2: A Missing Semicolon After a Variable Declaration
#include <iostream>
int main() {
int x = 5
std::cout << x;
}
GCC: error: expected ',' or ';' before 'std'
Clang: error: expected ';' at end of declaration
GCC mentions a comma because a declaration can continue with another variable (int x = 5, y = 10;). Either a comma or a semicolon would have been valid there. You want the semicolon: int x = 5;.
Cause 3: The Last Line Before a Closing Brace
The line just before a } still needs its semicolon. This one catches people out because the next line looks like the end of the code:
#include <iostream>
int add(int a, int b) {
return a + b
}
int main() {
std::cout << add(2, 3);
}
GCC: error: expected ';' before '}' token
Clang: error: expected ';' after return statement
Fix: return a + b;
Cause 4: A Missing Semicolon After a Class or Struct
This is the one that feels unfair. A function’s closing brace needs no semicolon, but a class’s closing brace does:
class Player {
int health = 100;
}
int main() {
Player p;
}
GCC: error: expected ';' after class definition
Clang: error: expected ';' after class
MSVC: error C2628: 'Player' followed by 'int' is illegal (did you forget a ';'?)
With struct the messages say “struct” instead of “class”.
Why the difference? C++ lets you declare variables straight after a class definition, before the semicolon:
struct Point {
int x, y;
} p1, p2; // declares a struct type AND two variables of that type
int main() {
p1.x = 3;
}
The ; tells the compiler the declaration is finished. Without it, the compiler tries to read the next line as more of the class declaration.
Fix: put ; after the closing brace — };
If the error points at a header file, look for this exact problem. A class at the bottom of player.h without its ; shows up like this when you compile main.cpp:
In file included from main.cpp:1:
player.h:3:2: error: expected ';' after class definition
The bug is in the header, not in main.cpp. See header files in C++ for how includes work.
Cause 5: A Missing Semicolon After an enum
Enums need a ; after the closing brace too. Clang tells you so plainly, but GCC produces one of its most confusing errors:
enum class Color { Red, Green, Blue }
int main() {
Color c = Color::Red;
}
GCC: error: new types may not be defined in a return type
error: two or more data types in declaration of 'main'
Clang: error: expected ';' after enum
GCC thinks you are defining a new enum type as the return type of main, because without the semicolon the two run together. If you ever see “new types may not be defined in a return type”, look for a missing ; after a } just above.
Fix: enum class Color { Red, Green, Blue }; — more in the enum tutorial.
Cause 6: The End of a do-while Loop
A while loop has no semicolon after its condition. A do-while loop does, because there the condition comes last:
#include <iostream>
int main() {
int i = 0;
do {
++i;
} while (i < 3)
std::cout << i;
}
GCC: error: expected ';' before 'std'
Clang: error: expected ';' after do/while statement
Fix: } while (i < 3); — see the do-while loop guide.
Cause 7: using namespace std Without a Semicolon
#include <iostream>
using namespace std
int main() {
cout << "Hi";
}
GCC: error: expected ';' before 'int'
Clang: error: expected ';' after namespace name
Fix: using namespace std; — and if you’re unsure whether to use that line at all, see using namespace std in C++.
Cause 8: Commas Instead of Semicolons in a for Loop
The three parts of a for loop are separated by semicolons, not commas:
#include <iostream>
int main() {
for (int i = 0, i < 5, ++i) {
std::cout << i;
}
}
GCC: error: expected ';' before '<' token
Clang: error: expected ';' in 'for' statement specifier
Fix: for (int i = 0; i < 5; ++i)
Visual Studio’s “expected a ’;’” When Nothing Is Missing
If Visual Studio says expected a ';' and every line has its semicolon, check your braces. A missing } at the end of one function means the next function ends up inside it:
#include <iostream>
int main() {
std::cout << "Hi";
// the closing } for main is missing here
void greet() {
std::cout << "Hello";
}
C++ doesn’t allow a function to be defined inside another function. Visual Studio’s IntelliSense flags the { after greet() with expected a ';', because inside a function, void greet() could only be the start of a declaration. When you build, the real problem gets clearer names:
MSVC: error C2601: 'greet': local function definitions are illegal
GCC: error: a function-definition is not allowed here before '{' token
Clang: error: function definition is not allowed here
Fix: add the missing } at the end of main. Clicking just after an opening brace highlights its partner in most editors, which makes the missing one easy to find.
Semicolons That Shouldn’t Be There
An extra semicolon causes problems too — and some of these compile without any error at all, which is worse.
After an if, for or while header. This compiles, but the semicolon is the body, so the block underneath always runs:
#include <iostream>
int main() {
int x = 3;
if (x > 5); { // the ; ends the if statement here
std::cout << "big\n"; // so this prints, even though 3 > 5 is false
}
}
Compiling with warnings on (-Wall -Wextra) catches it: GCC says suggest braces around empty body in an 'if' statement and Clang says if statement has empty body. The same mistake on a for loop gives you a loop that does nothing.
After a function header: int main(); { gives expected unqualified-id before '{' token.
At the end of a #define: #define SIZE 10; pastes the semicolon wherever you use SIZE, so int arr[SIZE]; becomes int arr[10;]; and fails with expected ']' before ';' token.
After an #include: #include <iostream>; only produces a warning (extra tokens at end of #include directive), but remove it anyway.
Where Semicolons Go: A Cheat Sheet
Needs a ; | No ; |
|---|---|
Statements: x = 5;, std::cout << x; | After #include and #define lines |
Declarations: int x = 5; | After if (...), for (...), while (...) |
return a + b; | Between a function header and its { |
After a class, struct or enum’s } | After a function’s closing } |
After do { } while (...) | After the } of an if, else or loop body |
using namespace std; | |
Function prototypes: int add(int a, int b); |
How to Find It Fast
- Fix the first error only, then recompile. One missing semicolon can set off a dozen follow-on errors that vanish once it’s fixed.
- Go to the line in the error, then look at the end of the line above.
- Read the token in the message. “expected ’;’ before ‘return’” means the
;belongs right before thatreturn. - “In file included from…” means the problem is in that header, usually a class missing its
;at the bottom. - If no semicolon is missing, count your braces. A missing
}causes the strangest messages of all.
Quick Reference
| Message | Compiler | Usual cause |
|---|---|---|
expected ';' before 'X' | GCC | Missing ; at the end of the line above X |
expected ',' or ';' before 'X' | GCC | Missing ; after a variable declaration |
expected ';' after class definition | GCC | Missing ; after a class’s } |
new types may not be defined in a return type | GCC | Missing ; after a class, struct or enum above a function |
expected ';' after expression | Clang | Missing ; at the end of a statement |
expected ';' at end of declaration | Clang | Missing ; after a variable declaration |
expected a ';' | Visual Studio | Missing ; — or a missing } before the next function |
C2143: syntax error: missing ';' before 'X' | Visual Studio | Missing ; before X, often on the line above |
C2628: 'A' followed by 'B' is illegal | Visual Studio | Missing ; after a class or struct |
For the other errors beginners hit most, see C++ error messages explained.
Take Your C++ Further
Syntax errors stop being scary once you understand what the compiler is actually reading. The C++ Better Explained Ebook explains the fundamentals in plain English, then the parts that really trip beginners up — pointers, memory and OOP. Just $19.
👉 Get the C++ Better Explained Ebook — $19
Related Articles
- C++ Error Messages Explained — the most common errors and what they mean.
- How to Fix ‘was not declared in this scope’ — the other error every beginner hits.
- C++ Classes and Objects — how class definitions work.
- Header Files in C++ — where many hidden missing semicolons live.
- C++ Loops Tutorial — for, while and do-while syntax.