Skip to content
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 struct or class definition
  • A macro or typedef without 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.

Why does the error often point to the wrong line?
The compiler reads tokens left to right. When it encounters an unexpected token (like return where a ; was expected), it reports the error at the unexpected token, not at the actual missing semicolon. Always look at the line(s) above where the error is reported.
Do all C++ statements end with a semicolon?
Not all. Control flow statements like if, while, for, and function definitions do not need semicolons after their bodies. However, expressions, declarations, return, break, continue, goto, and typedef all require semicolons. Class/struct/enum definitions need a semicolon after the closing brace.
Can a missing semicolon cause many cascading errors?
Yes. A single missing semicolon can produce dozens of confusing errors on subsequent lines. This is called error cascading. Always fix the first reported error first, recompile, and see how many remaining errors are actual problems.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro