Skip to content
C++ Better Explained
Go back
How to Fix Expected '}' at End of Input in C++

How to Fix “expected ’}’ at end of input” in C++

Short answer: a { in your file never got its closing }, and the compiler reached the end of the file still waiting for it. The error points at the last line, but the missing brace is earlier, usually at the end of a function, loop or if block. The note under the error points to the { that was never closed.

CompilerWhat you see
GCC (g++, MinGW, Code::Blocks)expected '}' at end of input, then note: to match this '{'
Clang (Xcode, macOS)expected '}', then note: to match this '{'
Visual Studiofatal error C1075: '{': no matching token found

Hit “expected '}' at end of input”? It's a classic beginner trap, and there are others like it.

Free 1-page checklist: The 10 Mistakes Every C++ Beginner Makes. It shows the exact traps that slow down every C++ beginner, so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


How to Read the Error

Here’s the smallest example, with the last } missing:

#include <iostream>

int main() {
    std::cout << "Hello\n";
    return 0;
GCC:    main.cpp:5:14: error: expected '}' at end of input
        main.cpp:3:12: note: to match this '{'
Clang:  main.cpp:5:14: error: expected '}'
        main.cpp:3:12: note: to match this '{'
MSVC:   main.cpp(3): fatal error C1075: '{': no matching token found

Two lines matter:

Fix: add the closing } after return 0;.

Cause 1: A Missing } Inside a Function

Usually the missing brace belongs to a loop or an if, not to the function itself:

#include <iostream>

int main() {
    for (int i = 0; i < 3; ++i) {
        std::cout << i << '\n';

    std::cout << "Done\n";
    return 0;
}
GCC:    main.cpp:9:2: error: expected '}' at end of input
        main.cpp:3:12: note: to match this '{'
Clang:  main.cpp:9:2: error: expected '}'
        main.cpp:3:12: note: to match this '{'
MSVC:   main.cpp(3): fatal error C1075: '{': no matching token found

The note points at main’s brace on line 3, even though the missing } belongs to the loop. The compiler pairs each } with the most recent unclosed {, so the } on the last line closed the loop, and main was left open.

So treat the note as a starting point. Begin at the brace it names and check each block inside it. The indentation gives this one away: the loop body should end after line 5, but nothing closes it.

Fix: close the loop where its body ends:

    for (int i = 0; i < 3; ++i) {
        std::cout << i << '\n';
    }

Cause 2: A Missing } Before else

When the missing brace is just before an else, GCC gives a more specific message:

#include <iostream>

int main() {
    int x = 5;
    if (x > 3) {
        std::cout << "big\n";
    else {
        std::cout << "small\n";
    }
    return 0;
}
GCC:    main.cpp:7:5: error: expected '}' before 'else'
        main.cpp:5:16: note: to match this '{'
Clang:  main.cpp:7:5: error: expected expression
        main.cpp:11:2: error: expected '}'
        main.cpp:3:12: note: to match this '{'
MSVC:   main.cpp(7): error C2181: illegal else without matching if
        main.cpp(3): fatal error C1075: '{': no matching token found

GCC is the most helpful here: its note points straight at the if block’s brace on line 5.

Fix: close the if block before the else: } else {. More in the conditionals tutorial.

Cause 3: A Function That Runs Into the Next One

If a function is missing its last }, the next function ends up inside it:

#include <iostream>

void greet() {
    std::cout << "Hello\n";

int main() {
    greet();
    return 0;
}
GCC:    main.cpp:6:12: error: a function-definition is not allowed here before '{' token
        main.cpp:9:2: error: expected '}' at end of input
        main.cpp:3:14: note: to match this '{'
Clang:  main.cpp:6:12: error: function definition is not allowed here
        main.cpp:9:2: error: expected '}'
        main.cpp:3:14: note: to match this '{'
MSVC:   main.cpp(6): error C2601: 'main': local function definitions are illegal
        main.cpp(3): note: this line contains a '{' which has not yet been matched
        main.cpp(3): fatal error C1075: '{': no matching token found

C++ doesn’t allow one function to be defined inside another, so the first error is about main, not about the missing brace. Whenever you see “function-definition is not allowed here”, check the function above it for a missing }.

Fix: add the } at the end of greet().

Want the fundamentals explained properly? C++ Better Explained covers the core of C++ in plain English, with analogies and code diagrams, including the parts that trip beginners up most: pointers, memory and OOP.

Get the book — $19

Cause 4: A Class Missing Its Closing Brace

#include <iostream>

class Counter {
public:
    void add() { ++count; }
private:
    int count = 0;

int main() {
    Counter c;
    c.add();
}
GCC:    main.cpp:12:2: error: expected '}' at end of input
        main.cpp:3:15: note: to match this '{'
        main.cpp:12:2: error: expected unqualified-id at end of input
Clang:  main.cpp:12:2: error: expected '}'
        main.cpp:3:15: note: to match this '{'
        main.cpp:12:2: error: expected ';' after class
MSVC:   main.cpp(3): fatal error C1075: '{': no matching token found

Without its closing brace, the class swallows main. Fix: close the class with }; before main. A class needs the semicolon after its brace as well; see expected ’;’ errors for why.

Cause 5: A Missing } in a Header File

If the missing brace is in a header, the error shows up in the file that includes it. Here shapes.h forgets to close its namespace:

// shapes.h
namespace shapes {

int area(int w, int h) {
    return w * h;
}
// main.cpp
#include <iostream>
#include "shapes.h"

int main() {
    std::cout << shapes::area(3, 4) << '\n';
}
GCC:    main.cpp:6:2: error: expected '}' at end of input
        In file included from main.cpp:2:
        shapes.h:1:18: note: to match this '{'
Clang:  main.cpp:6:2: error: expected '}'
        shapes.h:1:18: note: to match this '{'
MSVC:   shapes.h(1): fatal error C1075: '{': no matching token found

The note sends you to the header, so fix the brace there.

If the broken header is included before <iostream>, it’s much worse. The unclosed namespace swallows the standard library headers too, and you get a flood of strange errors from files like cwchar. In our test, GCC printed over 300 errors and Clang gave up after 20. If you ever see hundreds of errors inside standard headers, check your own headers for a missing brace first. See header files in C++.

How to Find the Missing Brace Fast

  1. Read the note, not just the error. GCC and Clang’s “to match this ’{’”, or the line number in Visual Studio’s C1075, tells you which block was left open.
  2. Let your editor format the file. Use Format Document: Shift+Alt+F in VS Code on Windows, or Ctrl+K, Ctrl+D in Visual Studio. Everything after the missing brace ends up indented one level too deep, which shows you where the block should have ended.
  3. Click next to a brace. Most editors highlight the matching brace, so you can check each { in turn.
  4. Type both braces at once. When you open a block, type { and } together, then fill in the middle. Many editors do this for you.

The Opposite Problem: One } Too Many

#include <iostream>

int main() {
    std::cout << "Hi\n";
}
}
GCC:    main.cpp:6:1: error: expected declaration before '}' token
Clang:  main.cpp:6:1: error: extraneous closing brace ('}')
MSVC:   main.cpp(6): error C2059: syntax error: '}'
        main.cpp(6): error C2143: syntax error: missing ';' before '}'

Fix: delete the extra } on the line the error points to.

Quick Reference

MessageUsual cause
expected '}' at end of input (GCC), expected '}' (Clang)A { with no matching }; the note shows which one
C1075: '{': no matching token found (Visual Studio)The same; the line number is the unclosed {
expected '}' before 'else'Missing } at the end of an if block
a function-definition is not allowed hereThe function above is missing its }
expected declaration before '}' tokenOne } too many
Hundreds of errors inside standard headersA header with a missing }, included before them

For other common messages, see C++ error messages explained.


Take Your C++ Further

C++ Better Explained ebook cover

If you'd like the fundamentals explained in one place, C++ Better Explained covers them in 87 plain-English pages, with analogies and code diagrams, including the parts that trip beginners up most: pointers, memory and OOP.

Get the book — $19

87 pages · plain English · 30-day money-back guarantee


📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
How to Fix C4996 'This function or variable may be unsafe' in Visual Studio
Next Post
How to Fix 'lvalue required as left operand of assignment' in C++

Keep Learning