Skip to content
C++ Better Explained
Go back
How to Fix 'expected primary-expression before' in C++

How to Fix “expected primary-expression before” in C++

Short answer: the compiler needed a value — a variable, a number, a function call — and found something else instead: a type name like int, an empty ], or nothing at all. The token after “before” tells you exactly where to look.

The same mistake gets different wording on each compiler:

CompilerWhat you see
GCC (g++, MinGW, Code::Blocks)expected primary-expression before 'int'
Clang (Xcode, macOS)expected expression, or expected '(' for function-style cast or type construction
Visual StudioC2144: syntax error: 'int' should be preceded by ')', or C2059: syntax error

What’s a “Primary Expression”?

It’s the simplest building block of an expression — something that has a value:

So “expected primary-expression before ‘int’” means: I needed a value here, but the next thing I found was int — which is a type, not a value. Below are the seven ways that usually happens.

Cause 1: Putting Types in a Function Call

This is the big one. When you define a function, you list the types of its parameters. When you call it, you don’t:

#include <iostream>

int add(int a, int b) {
    return a + b;
}

int main() {
    int x = 2, y = 3;
    int sum = add(int x, int y);
    std::cout << sum << '\n';
}
GCC:    error: expected primary-expression before 'int'
Clang:  error: expected '(' for function-style cast or type construction
MSVC:   error C2144: syntax error: 'int' should be preceded by ')'

Fix: pass just the values — int sum = add(x, y);

The types are already known from the function’s definition, so the call only needs the values. More on this in the functions tutorial.

Learning C++ from scratch? The C++ Better Explained Ebook explains the fundamentals — and the parts that trip most beginners up, like pointers, memory and OOP — in 87 plain-English pages. Just $19.

Cause 2: Passing an Array With Empty Brackets

The same mix-up, with arrays:

#include <iostream>

void printAll(int arr[], int size) {
    for (int i = 0; i < size; ++i) std::cout << arr[i] << ' ';
}

int main() {
    int nums[3] = {1, 2, 3};
    printAll(nums[], 3);
}
GCC:    error: expected primary-expression before ']' token
Clang:  error: expected expression
MSVC:   error C7732: expected an expression before ']'

Fix: printAll(nums, 3); — pass the array’s name on its own. The empty [] belongs only in the parameter list. See how to pass an array to a function.

Cause 3: A Missing Value After an Operator

An operator with nothing after it:

#include <iostream>

int main() {
    int price = 20;
    int total = price * ;
    std::cout << total;
}
GCC:    error: expected primary-expression before ';' token
Clang:  error: expected expression
MSVC:   error C2059: syntax error: ';'

The same thing happens with a dangling << — std::cout << "Hello" << ; gives the identical message. Fix: finish the expression, e.g. int total = price * quantity;

Cause 4: An Unfinished Condition

#include <iostream>

int main() {
    int x = 5;
    if (x > 0 && ) {
        std::cout << "positive";
    }
}
GCC:    error: expected primary-expression before ')' token
Clang:  error: expected expression
MSVC:   error C2059: syntax error: ')'

Fix: give && its second half — if (x > 0 && x < 10).

Cause 5: An Extra Semicolon in a for Loop

A for loop has exactly two semicolons inside its parentheses. A third one confuses the parser:

#include <iostream>

int main() {
    for (int i = 0; i < 3; i++;) {
        std::cout << i;
    }
}
GCC:    error: expected ')' before ';' token
        error: expected primary-expression before ')' token
Clang:  error: unexpected ';' before ')'
MSVC:   error C2059: syntax error: ';'

Fix: for (int i = 0; i < 3; i++)

Cause 6: Using a Type Name Instead of a Variable

Calling a function on the type rather than on your variable:

#include <iostream>
#include <string>

int main() {
    std::string name = "Sam";
    std::cout << std::string.length();
}
GCC:    error: expected primary-expression before '.' token
Clang:  error: expected '(' for function-style cast or type construction
MSVC:   error C2275: 'std::string': expected an expression instead of a type

Fix: call it on the variable — name.length(). It’s the same mistake with your own classes: Player.getName() instead of player.getName(). The class is the blueprint; the object is the thing that has a name.

Cause 7: A Missing template Keyword (Templates Only)

If you’re not writing templates yet, skip this one. Inside a template, calling a member function template on an object whose type depends on a template parameter needs the template keyword:

#include <iostream>

struct Settings {
    template <typename T> T get() const { return T{}; }
};

template <typename Config>
void printValue(const Config& c) {
    std::cout << c.get<int>() << '\n';
}

int main() {
    printValue(Settings{});
}
GCC:    error: expected primary-expression before 'int'
Clang:  error: use 'template' keyword to treat 'get' as a dependent template name

Clang names the fix for you: write c.template get<int>(). Without it, the compiler reads the < as a less-than sign. More in templates explained.

How to Read the Message

The token after “before” is your best clue:

Message ends withUsually means
before 'int' (or another type)A type where a value goes — usually inside a function call
before ']' tokenEmpty brackets — usually passing an array as arr[]
before ';' tokenSomething’s missing at the end of the line
before ')' tokenSomething’s missing at the end of a condition or call
before '.' tokenA type name used as if it were a variable

Always fix the first error and recompile — one of these can trigger several follow-on errors that disappear once it’s fixed. For more messages and what they mean, see C++ error messages explained.


Take Your C++ Further

Errors like this come from the difference between declaring things and using them — the idea that makes functions, arrays and classes click. The C++ Better Explained Ebook explains it all in plain English, including the parts that trip beginners up most: pointers, memory and OOP. Just $19.

👉 Get the C++ Better Explained Ebook — $19


📋

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 'g++ is not recognized' on Windows

Keep Learning