If you've encountered an error like this while compiling C++:

error: expected primary-expression before 'int' 

or:

error: expected primary-expression before ')' token 

you're not alone. These errors can look confusing, especially when the compiler points to a line that seems almost correct.

The good news is that they usually come down to a small syntax mistake.

In this guide, we'll look at what the error means, three common causes, and how to fix them with practical C++ examples.

What does "expected primary-expression" mean?

In C++, a primary expression is a basic part of an expression that represents or produces a value. Examples include:

  • A variable, such as number
  • A literal, such as 42
  • A function call, such as calculate(5)
  • A parenthesized expression, such as (a + b)

When the compiler reports an expected primary-expression error, it has encountered something that doesn't fit where it expects an expression.

The diagnostic often points to the token where parsing failed, but the actual mistake may appear earlier in the statement.

Let's look at some examples.

1. Passing a type instead of a value

One common mistake is including a parameter's type when calling a function.

Incorrect code

#include <iostream> int area(int width) { return width * width; } int main() { std::cout << area(int width) << '\n'; return 0; } 

With GCC, this produces:

error: expected primary-expression before 'int' 

Why does this happen?

When defining the function, we declare its parameter using int width.

However, when calling the function, we need to pass an actual value or expression rather than repeat the parameter declaration.

Correct code

#include <iostream> int area(int width) { return width * width; } int main() { int width = 4; std::cout << area(width) << '\n'; return 0; } 

Output:

16 

Remember: Function declarations specify parameter types. Function calls supply argument expressions.

2. Missing expressions or misplaced parentheses

Another common cause is an incomplete expression.

Consider this example:

#include <iostream> int main() { std::cout << (5 + 2 * ) << '\n'; return 0; } 

The multiplication operator * requires an operand on its right.

Instead, the compiler encounters a closing parenthesis.

GCC reports:

error: expected primary-expression before ')' token 

Correct code

#include <iostream> int main() { std::cout << (5 + 2 * 3) << '\n'; return 0; } 

Output:

11 

When investigating similar errors, check for missing operands, mismatched parentheses, extra commas, and incomplete function calls.

Sometimes a missing delimiter earlier in the statement causes the compiler to report an error at a later token.

3. Using a type where an object is required

Another mistake involves calling an instance member function on a type rather than an object.

Incorrect code

#include <iostream> #include <string> int main() { std::cout << std::string.size() << '\n'; return 0; } 

GCC reports:

error: expected primary-expression before '.' token 

Here, std::string identifies a type, not an individual string object.

The size() function must be called on an actual string object.

Correct code

#include <iostream> #include <string> int main() { std::string text = "cat"; std::cout << text.size() << '\n'; return 0; } 

Output:

3 

The key distinction is between a type, which describes objects, and an object, which can hold values and expose instance members.

How to debug this error efficiently

When you encounter an expected primary-expression error, try the following:

  1. Read the first compiler diagnostic. Later errors may be consequences of an earlier syntax mistake.
  2. Inspect the preceding tokens. The actual problem may be just before the compiler's highlighted location.
  3. Check function calls. Make sure you're supplying values instead of repeating parameter declarations.
  4. Check operators and parentheses. Look for missing operands, unmatched delimiters, or misplaced commas.
  5. Check object usage. Make sure instance members are accessed through objects rather than type names.
  6. Fix one issue and compile again. Avoid adding random semicolons or casts simply to suppress a diagnostic.

Compiler wording can differ between GCC, Clang, and MSVC, so focus on the surrounding C++ syntax rather than memorizing one particular error message.

Final thoughts

The message "expected primary-expression" doesn't identify one specific mistake. It means the compiler expected an expression and encountered something that couldn't be parsed as one.

Understanding the difference between types and values, checking parentheses, and reading the first diagnostic carefully will help you resolve these errors faster.

For additional examples and related compiler-error explanations, see the original expected primary-expression guide on CppPath.

This article is published by CppPath, a C++ educational learning platform. It was prepared with AI assistance, and the main code examples were checked using GCC with C++17.