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: a colon is missing where C++ requires one: after case 1 or default in a switch, after public or private in a class, or between the two choices of ? :. Most of the time a semicolon was typed where the colon belongs.

WhereWhat you see
Visual Studio editor (red squiggle)expected a ':'
Visual Studio buildC2143: syntax error: missing ':' before ';'
GCC (g++, MinGW, Code::Blocks)expected ':' before ';' token
Clang (Xcode, macOS)expected ':' after 'case'

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: case 1; Instead of case 1:

#include <iostream>

int main() {
    int choice = 1;
    switch (choice) {
        case 1;
            std::cout << "One\n";
            break;
    }
}
Visual Studio editor:  expected a ':'
MSVC build:            error C2143: syntax error: missing ':' before ';'
GCC:                   error: expected ':' before ';' token
Clang:                 error: expected ':' after 'case'

A case label ends with a colon. It marks where that case’s code begins. Fix: case 1:, and the program prints One.

Cause 2: default; Instead of default:

#include <iostream>

int main() {
    int choice = 3;
    switch (choice) {
        case 1:
            std::cout << "One\n";
            break;
        default;
            std::cout << "Other\n";
    }
}
Visual Studio editor:  expected a ':'
MSVC build:            error C2065: 'default': undeclared identifier
GCC:                   error: expected ':' before ';' token
Clang:                 error: expected ':' after 'default'

Visual Studio’s build message is misleading here. default is a keyword, not an identifier, so trust the squiggle. Fix: default:, which prints Other. More in the switch statement guide.

Classes and inheritance feeling abstract? C++ Better Explained explains object-oriented C++ in plain English, with analogies and code diagrams, from your first class to inheritance and polymorphism.

Get the book — $27

Cause 3: public Without a Colon

#include <iostream>

class Player {
public
    int score;
};

int main() {
    Player p;
    p.score = 5;
    std::cout << p.score << '\n';
}
Visual Studio editor:  expected a ':'
MSVC build:            error C2062: type 'int' unexpected
                       error C2039: 'score': is not a member of 'Player'
GCC:                   error: expected ':' before 'int'
Clang:                 error: expected ':'

public, private and protected are labels inside a class, and each one ends with a colon. Fix: public:, and the program prints 5. See classes and objects for more.

Cause 4: A ?: Operator Missing Its Colon

#include <iostream>

int main() {
    int a = 4, b = 9;
    int bigger = (a > b) ? a b;
    std::cout << bigger << '\n';
}
Visual Studio editor:  expected a ':'
MSVC build:            error C2146: syntax error: missing ',' before identifier 'b'
GCC:                   error: expected ':' before 'b'
Clang:                 error: expected ':'
                       note: to match this '?'

The ternary operator always has both parts: condition ? value_if_true : value_if_false. Fix: (a > b) ? a : b, which gives 9. More in the ternary operator guide.

This one doesn’t say “expected a ’:’”, but it’s the same kind of slip:

#include <iostream>

int main() {
    std:cout << "Hi\n";
}
Visual Studio editor:  identifier "cout" is undefined
MSVC build:            error C2065: 'cout': undeclared identifier
GCC:                   error: 'cout' was not declared in this scope; did you mean 'std::cout'?
                       warning: label 'std' defined but not used

With one colon, std: is read as a label (a name you could jump to with goto), so cout is looked up on its own and isn’t found. GCC’s warning gives it away. Fix: two colons, std::cout. The scope resolution operator is always ::.

Quick Reference

You wroteFix
case 1;case 1:
default;default:
public (in a class)public:
x ? a bx ? a : b
std:coutstd::cout

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


Take Your C++ Further

C++ Better Explained ebook cover

Object-oriented C++ clicks once you have a clear picture of what an object is and how classes relate to each other. C++ Better Explained covers classes, inheritance and polymorphism in plain English, with analogies and code diagrams.

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.


Previous Post
How to Fix 'expected a file name' in C++ (#include Errors)
Next Post
How to Fix "expected a '{'" in C++ (Visual Studio)

Keep Learning