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

How to Fix “expected a declaration” in C++ (Visual Studio)

Short answer: Visual Studio found a statement, such as return, if, for or a lone }, sitting outside every function. Outside a function C++ only allows declarations: variables, functions, classes. Nine times out of ten, an extra } a few lines up closed the function early, so the code after it is now “outside”. Find that brace and delete it.

WhereWhat you see
Visual Studio editor (red squiggle)expected a declaration
Visual Studio buildC2059: syntax error: '}', or C2447: '{': missing function header
GCC (g++, MinGW, Code::Blocks)expected unqualified-id before 'return', or expected declaration before '}' token
Clang (Xcode, macOS)expected unqualified-id, or extraneous closing brace ('}')

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


Why C++ Says “Declaration”

A C++ file has two levels. At the top level, outside any function, you can only declare things: int count = 0;, int add(int a, int b) { ... }, class Player { ... };. Statements that do something, such as return, if, loops and std::cout << ..., have to live inside a function body.

So “expected a declaration” means: “I’m at the top level, and this isn’t something you can declare.” The question is how your code ended up at the top level.

Cause 1: An Extra } Ends main Too Early

#include <iostream>

int main() {
    int lives = 3;
    if (lives > 0) {
        std::cout << "Still playing\n";
    }}
    std::cout << "Game over\n";
    return 0;
}

The second } on line 7 closes main. Everything after it is outside the function:

Visual Studio editor:  this declaration has no storage class or type specifier   (line 8)
                       expected a declaration                                  (line 9)
GCC:                   error: 'cout' in namespace 'std' does not name a type
                       error: expected unqualified-id before 'return'
                       error: expected declaration before '}' token
Clang:                 error: no type named 'cout' in namespace 'std'
                       error: expected unqualified-id
                       error: extraneous closing brace ('}')

The first error is on line 8, but the mistake is on line 7. That’s typical: the errors start after the extra brace.

Fix: delete the extra }:

#include <iostream>

int main() {
    int lives = 3;
    if (lives > 0) {
        std::cout << "Still playing\n";
    }
    std::cout << "Game over\n";
    return 0;
}
Still playing
Game over

Cause 2: A Semicolon After int main()

#include <iostream>

int main(); {
    std::cout << "Hello\n";
    return 0;
}
Visual Studio editor:  expected a declaration
MSVC build:            error C2447: '{': missing function header (old-style formal list?)
GCC:                   error: expected unqualified-id before '{' token
Clang:                 error: expected unqualified-id

The ; ends int main() as a declaration (“a function called main exists”), so the { after it belongs to nothing. Visual Studio’s build message says exactly that: a body with a missing function header.

Fix: remove the semicolon: int main() {. The program then prints Hello.

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: Code Written Outside Any Function

Beginners sometimes write code straight into the file, above or below main:

#include <iostream>

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

int main() {
}
Visual Studio editor:  expected a declaration
MSVC build:            error C2059: syntax error: 'for'
GCC:                   error: expected unqualified-id before 'for'
Clang:                 error: expected unqualified-id

Fix: move it inside main (or another function):

#include <iostream>

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

An assignment outside a function, such as count = 5;, gets a different message, this declaration has no storage class or type specifier, but the fix is the same: move it into a function.

Cause 4: An Extra } After a Function

#include <iostream>

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

int main() {
    greet();
}
Visual Studio editor:  expected a declaration
MSVC build:            error C2059: syntax error: '}'
GCC:                   error: expected declaration before '}' token
Clang:                 error: extraneous closing brace ('}')

Clang’s wording says it best: there’s a closing brace with nothing to close. Fix: delete it. This often happens after deleting a block of code but leaving its brace behind.

How to Find the Extra Brace

  1. Start just above the first error. The extra } is almost always within a few lines of it.
  2. Click next to each { and }. Visual Studio highlights the partner brace. One that pairs with the wrong brace, or with nothing, is the culprit.
  3. Format the file with Ctrl+K, Ctrl+D. After an extra }, the code that should be inside the function loses its indentation, which shows you where things went wrong.

The opposite problem, a { that never gets closed, is covered in expected ’}’ at end of input.

Quick Reference

What you see in the codeFix
}} or a } that closes main earlyDelete the extra }
int main(); {int main() {
A loop, if or std::cout above mainMove it inside a function
A stray } between functionsDelete it

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 'was not declared in this scope' in C++
📋

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.


Next Post
How to Fix 'expected an expression' in C++ (Visual Studio)

Keep Learning