expected ';' before '...'
expected ';' before '...'
DodaTech
3 min read
The expected ';' before '...' error means the compiler expected a semicolon where it found something else. In C/C++, semicolons terminate most statements.
What It Means
The compiler’s parser reached a token it did not expect. The previous statement likely lacks its terminating semicolon. The error location can be misleading — the actual problem is often on the line above.
Why It Happens
- Missing semicolon at the end of a statement
- Class, struct, or enum definition missing a semicolon after the closing brace
- Missing semicolon after a
structorclassdefinition - A macro or
typedefwithout a semicolon - Copy-paste errors that break statement boundaries
How to Fix It
Step 1: Check the line before the error
#include <iostream>
int main() {
int a = 5 // missing semicolon!
int b = 10;
std::cout << a + b << std::endl;
return 0;
}g++ semicolon.cpp -o semicolon
# error: expected ';' before 'b'Fix: Add ; after int a = 5.
Step 2: Add semicolon after class/struct definition
#include <iostream>
struct Point {
int x;
int y;
} // missing semicolon!
int main() {
Point p;
return 0;
}// fixed
struct Point {
int x;
int y;
};Step 3: Fix missing semicolon in typedef
typedef struct {
int id;
char name[50];
} Employee // missing semicolon!
// fixed
typedef struct {
int id;
char name[50];
} Employee;Step 4: Check for missing semicolons in macros
#define SQUARE(x) ((x) * (x)) // no semicolon in macro definition
int main() {
int result = SQUARE(5) // missing semicolon!
return 0;
}// fixed
int main() {
int result = SQUARE(5);
return 0;
}Step 5: Use compiler error location hints
The compiler often points to the token that confused it:
error: expected ';' before 'return'
12 | return 0;
| ^~~~~~The actual missing semicolon is near line 11, but the compiler reports the error when it hits return on line 12. Always look at the line before the error location first.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro