Compiler and linker errors

C++ "expected ';' before": find the missing semicolon

The compiler reports an expected-semicolon error when a statement or declaration ends without the required ;. It often highlights the next token because that is where parsing became impossible, so inspect the preceding line first. A missing semicolon after a class or struct definition is another common cause.

Read the location

Read the full first diagnostic and then look at the source line it names. The highlighted token may be correct. The parser may have been expecting a statement terminator on the line above before it could understand the token below. If your editor reports a line and column, scan both directions around that position instead of inserting punctuation at once.

Compilers recover from syntax errors to report more issues, but later messages can be consequences of the first mistake. Fix one structural error and compile again. GCC wording depends on the surrounding grammar and version. MSVC commonly uses C2143 for an expected token such as a semicolon. The diagnostic code points to a syntax problem; the source context identifies the missing character.

Finish statements

Most declarations and expression statements end with a semicolon. Examples include a variable declaration, assignment, function call, return statement, and an empty statement. A control statement header such as if (ready) does not take a semicolon before its body. Adding one there can create an empty body and change program behavior, so distinguish the header from the statement inside its braces.

In this failing program, the declaration of count lacks its terminator. The error may point at std::cout, the next token the parser cannot use.

#include <iostream>

int main() {
    int count = 3
    std::cout << count << '\n';
    return 0;
}

GCC diagnostic core:

error: expected ',' or ';' before 'std'

GCC lists both , and ; here because a declaration can continue with another variable, as in int count = 3, total = 0;. After an expression statement, such as an assignment, GCC usually reports only expected ';' before. In both cases, the message names the next token, std, rather than the line that is missing the semicolon. The exact wording and location vary by compiler and version. MSVC commonly reports C2143. Add the semicolon to the declaration that ends before the highlighted output statement.

#include <iostream>

int main() {
    int count = 3;
    std::cout << count << '\n';
    return 0;
}

Expected output:

3

Find a missing statement semicolon

The final statement in a block still needs its semicolon. GCC reports the token where it discovers that the statement did not end.

Failing example: missing semicolon before the closing brace

int main() {
    return 0
}

GCC diagnostic core:

error: expected ';' before '}' token

The exact wording varies by compiler and version. MSVC commonly reports C2143 for a missing semicolon before a closing brace.

End class and struct definitions

A class or struct definition ends with a semicolon after its closing brace. The semicolon belongs after the brace, even when the type body contains member declarations that each have their own semicolons. A missing final semicolon can make the next declaration appear to be part of an unfinished type definition.

Do not add semicolons after every closing brace. Function definitions and control-flow blocks do not take one after the brace. The required punctuation depends on the grammar: declarations need terminators, while compound statements use braces. Check a nearby example with the same syntax when you are unsure.

Also check matching parentheses, brackets, and braces. A missing closing parenthesis can shift the parser's understanding and produce an expected-semicolon message farther down. Balance delimiters before moving punctuation. Use indentation to see which block each brace closes, and inspect the first diagnostic rather than trying to silence every later message.

After the code compiles, inspect behavior as well. A semicolon after an if condition creates a separate empty statement. That code can compile while the intended block runs unconditionally. Syntax correctness and control-flow intent are separate checks.

When the error points into a macro expansion or a long expression, inspect the original source around the macro call as well as the expanded location. A missing separator inside a macro can make a later statement look malformed. Keep macro definitions small, and prefer a function when ordinary C++ syntax can express the operation. This makes diagnostics point closer to the source that needs attention.

After a semicolon fix, review the surrounding braces and indentation. The parser can recover differently after each correction, so a newly reported location may be a separate issue. Format the code consistently and rebuild after the first error is fixed. Avoid inserting a semicolon at the exact cursor position until you have checked whether the preceding construct is a statement, a type definition, or a block.

Find a missing closing brace

A closing brace ends a function or control-flow block. If the source ends before a required brace, GCC reports expected '}' at end of input. Check the opening braces and indent their matching closing braces before changing nearby semicolons.

Failing example: main has no closing brace

#include <iostream>

int main() {
    std::cout << "Done\n";

GCC diagnostic core:

error: expected '}' at end of input

The exact wording varies by compiler and version. MSVC reports C1075 when the file ends before the matching brace.

Fixed example

#include <iostream>

int main() {
    std::cout << "Done\n";
    return 0;
}

Expected output:

Done

Match each opening brace with its closing brace at the same indentation level.

Exercise

Fix the missing semicolon after the struct definition. Read a direction and distance, then print them on one line. Sample input:

east 5

Failing exercise

#include <iostream>
#include <string>

struct Route {
    std::string direction;
    int distance;
}

int main() {
    Route route;
    if (!(std::cin >> route.direction >> route.distance)) {
        return 1;
    }
    std::cout << route.direction << ": " << route.distance << '\n';
    return 0;
}

GCC diagnostic core for example 2:

error: expected ';' after struct definition

The exact wording varies by compiler and version. MSVC reports C2628 for this case: the type name is followed by ‘int’, and the message asks whether a ‘;’ is missing. Expected output after the fix:

east: 5

Hint 1: A type definition ends after its closing brace. Hint 2: Put the terminator after the brace.

Show one possible solution
#include <iostream>
#include <string>

struct Route {
    std::string direction;
    int distance;
};

int main() {
    Route route;
    if (!(std::cin >> route.direction >> route.distance)) {
        return 1;
    }
    std::cout << route.direction << ": " << route.distance << '\n';
    return 0;
}

Keep learning with CppPath

Continue with the Compiler Errors & Warnings lesson. The full lesson includes a quiz and a checked coding challenge with a free account.