Skip to content
C++ Better Explained
Go back
How to Fix "expected a '{'" in C++ (Visual Studio)

How to Fix “expected a ‘{’” in C++ (Visual Studio)

Short answer: something that must have a body in curly braces doesn’t have one. Functions, namespaces and try blocks always need { }, even for a single line. Most often, a function’s opening { was deleted or never typed.

WhereWhat you see
Visual Studio editor (red squiggle)expected a '{'
Visual Studio buildC2143: syntax error: missing ';' before ...
GCC (g++, MinGW, Code::Blocks)expected initializer before 'std'
Clang (Xcode, macOS)expected function body after function declarator

Hit “expected a '{'”? 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.


Cause 1: A Function Missing Its Opening Brace

#include <iostream>

int main()
    std::cout << "Hello\n";
    return 0;
}
Visual Studio editor:  expected a '{'
MSVC build:            error C2143: syntax error: missing ';' before 'std::cout'
GCC:                   error: expected initializer before 'std'
                       error: expected unqualified-id before 'return'
                       error: expected declaration before '}' token
Clang:                 error: expected function body after function declarator
                       error: expected unqualified-id
                       error: extraneous closing brace ('}')

Clang’s first line says it best: after int main() it expected the function’s body. The later errors, including an “extra” } at the end, all come from the same missing brace. Fix:

#include <iostream>

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

Cause 2: A One-Line Function Without Braces

if and loops can skip the braces for a single statement. Functions can’t:

#include <iostream>

int main() return 0;
Visual Studio editor:  expected a '{'
MSVC build:            error C2143: syntax error: missing ';' before 'return'
                       error C2059: syntax error: 'return'

Fix: int main() { return 0; }. The braces are part of every function definition, however short.

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 — $27

Cause 3: A namespace Without Braces

#include <iostream>

namespace game
    int score = 0;
}

int main() {
}
Visual Studio editor:  expected a '{'
MSVC build:            error C2062: type 'int' unexpected
GCC:                   error: expected '{' before 'int'

A namespace wraps its contents in braces:

#include <iostream>

namespace game {
    int score = 0;
}

int main() {
    std::cout << game::score << '\n';
}
0

See the namespace tutorial for more.

Cause 4: try Without Braces

#include <iostream>

int main() {
    try
        throw 1;
    catch (int e) {
        std::cout << "Caught\n";
    }
}
Visual Studio editor:  expected a '{'
MSVC build:            error C2319: 'try' must be followed by a compound statement. Missing '{'
GCC:                   error: expected '{' before 'throw'
Clang:                 error: expected '{'

try and catch always need braces, even around one line. Fix:

#include <iostream>

int main() {
    try {
        throw 1;
    }
    catch (int e) {
        std::cout << "Caught " << e << '\n';
    }
}
Caught 1

More in exception handling in C++.

Quick Reference

You wroteFix
int main() then code, no {int main() {
int main() return 0;int main() { return 0; }
namespace game then codenamespace game { ... }
try then one linetry { ... }

The opposite problem, a { that’s never closed, has its own guide: expected ’}’ at end of input. 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 — $27

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


Next common error How to Fix "expected a ':'" in C++ (Visual Studio)
📋

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 "expected a ':'" in C++ (Visual Studio)
Next Post
How to Fix "expected a '('" in C++ (Visual Studio)

Keep Learning