C++

C++ Expected Unqualified-Id Fix

A declaration names what it creates: a type, then the name of the thing. When the token sitting in the name's place is a keyword, a number, a macro's expansion, or nothing at all, compilation stops with 'expected unqualified-id before ...'. The type is rarely the problem; the token in the name slot is. This guide separates the cases, reads what the diagnostic actually points at, and matches each one to its fix.

Written November 25, 2026 · Tested with GCC 13.3.0 · 12 min read

By the end of this guide
  • The rule underneath: the name slot accepts an identifier, and identifiers have rules
  • How to read the diagnostic
  • The first failure: a keyword used as a variable name
  • The second failure: a keyword used as a member name

Written November 25, 2026

Tested with GCC 13.3.0

What this guide is

Every declaration has a slot where the new name goes. After the type in int total = 5;, the parser expects an identifier - the name being declared. The grammar calls that name an unqualified-id: an identifier, possibly with operator or template decoration, but not qualified by a scope. When the token in that slot is something that can never be a name - a keyword such as new or return, a numeric literal, the = that would have followed the name, or the ; ending an expression that stopped after :: - compilation stops with "expected unqualified-id before ...", and the token named at the end of the message is the one sitting in the name's place.

The causes in this guide each reproduce the failure with a small, complete program: a keyword used as a variable name, a keyword used as a class member name, a macro expanding into the name slot of a declaration, a declaration with the name left out entirely, a number written where the name belongs, and a scope resolution operator with nothing after it. In each family the failing program is small, the compiler output is shown, and the fix puts a legal name - or the missing name - into the slot.

Build every program in this guide with g++ -std=c++20 -Wall -Wextra. The failing programs are compiled with -c - compile only - because the error prevents any program from being produced; the fixed programs are compiled and run, so their output is shown.

This guide is about the name slot in declarations and qualified names. Nearby failures that involve a name-shaped problem but report a different sentence - a keyword in a parameter list, a literal after ::, a missing value or a missing semicolon - are at the end in the traps, with the wording each one actually gets, because the repair for those is different and searching on the wrong sentence wastes time.

The rule underneath: the name slot accepts an identifier, and identifiers have rules

An identifier is a sequence of letters, digits and underscores whose first character is not a digit. Identifiers name objects, functions, types, class members, namespaces and the other entities a program declares, with one hard exception stated on that page: the identifiers that are keywords cannot be used for other purposes. The keyword list is closed - new, delete, this, return, template and the rest are reserved words the language uses for itself, "not available for re-definition or overloading". A keyword token is therefore never an identifier, no matter which declaration it is offered to, and offering one is the first two families below.

Three consequences follow. First, the fix for a keyword in the name slot is always a rename, never a workaround: there is no escaping, quoting or scoping that lets a keyword serve as a name, and the replacement only has to differ from the keyword - newer, fresh, new_count are all ordinary identifiers. Second, the name slot is filled before the rest of the declaration is read, which is why the token named in the message is so often a follower rather than the culprit: in int = 5; the message names =, because the parser consumed the type, looked for the name, and found the initializer's first token instead. Read the message as "the name should have been immediately before this token". Third, the slot is filled after preprocessing. A macro name in the source is replaced by its replacement list before the parser sees the declaration, so the token in the slot can be one that appears nowhere near the declaration in the file - the third family, where the compiler points at the #define line itself.

The same slot appears after a scope resolution operator. A qualified name is a name that appears on the right-hand side of ::, and that right-hand side is again an unqualified-id - app::value is the qualifier app:: plus the unqualified name value. Ending the expression right after the :: leaves that slot empty, which is the sixth family.

How to read the diagnostic

Every failure in the main families prints the same shape of output: the file and the enclosing function on the first line, then an error line giving the line and column of the offending token, the message "expected unqualified-id before ..." naming that token, and the source line with a caret under the token. Read it in order. The token after "before" is the whole diagnosis: a quoted keyword ('new', 'return') means a reserved word was offered as a name; "numeric constant" means a number is in the slot, either written there or put there by a macro; a quoted punctuation token ('=' token, ';' token, ')' token) means the name is simply absent and the parser ran into whatever came next. The location matters as much as the token. When the error line points at a #define line and a note underneath says "in expansion of macro", the declaration line named in the note is where the name was supposed to go, and the real culprit is the macro - the third family. When the error points inside a class definition, the member declaration is the broken line even if the program also uses the member later - later uses of a keyword member report the same sentence at each access, as the second family shows in its probes. One more reading habit: this message is a parse error, so the compiler recovers and keeps going, and follow-on errors in the same function often vanish once the name slot is fixed. Repair the first "expected unqualified-id" in the output before chasing any error printed after it.

The first failure: a keyword used as a variable name

A program that names a variable new, a natural word for a fresh value:

C++
int main() {
    int new = 5;
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o fail_keyword.o fail_keyword.cpp
fail_keyword.cpp: In function ‘int main()’:
fail_keyword.cpp:2:9: error: expected unqualified-id before ‘new’
    2 |     int new = 5;
      |         ^~~

new is on the keyword list - it is the operator that creates objects in dynamic storage - so it cannot be the name of anything. The parser read the type int, looked for the identifier that names the variable, and found the keyword instead. This is not specific to new: the same shape with this, delete or template in the slot reports the same sentence naming that keyword, because the rule is about the whole reserved list, not about any one word. Code that arrives here has usually ported a name from a language with fewer reserved words, or shortened a longer name - new_total, new_value - down to the keyword itself.

Rename the variable to a word that is not reserved. Any identifier that differs from the keyword works:

C++
#include <iostream>
int main() {
    int fresh = 5;
    std::cout << fresh << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o fix_keyword fix_keyword.cpp
$ ./fix_keyword
5

The output is 5. Two naming habits keep this family away: keep the keyword visible as a prefix or suffix of a longer identifier (new_count, is_new), which is an ordinary name because the whole token differs from the keyword; and remember that case matters - New is not the keyword new, since identifiers are case-sensitive - though names that differ from a keyword only by case are legal and still a poor idea, because every reader has to check which one they are looking at.

The second failure: a keyword used as a member name

A struct that gives a field the name of the value it holds - the result of an operation, named return after what a form or a report calls it:

C++
struct Config {
    int return;
};
int main() {
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o fail_member.o fail_member.cpp
fail_member.cpp:2:9: error: expected unqualified-id before ‘return’
    2 |     int return;
      |         ^~~~~~

A member declaration has the same name slot as a variable declaration, and return is a keyword, so the slot rejects it for the same reason as the first family. The difference is reach: the name is rejected at the declaration, and every later access written as c.return reports the same sentence again at the access, because the parser meets the keyword after the dot as well. A struct copied from a data format whose field names come from another language - JSON payloads and report layouts are the usual sources - can carry several such names at once (new and return are both plausible field labels), and each one fails separately. A field labeled class fails too, but with the different wording shown in the traps, because class redirects the parse before the name slot is reached.

Rename the member, and use the new name at every access:

C++
#include <iostream>
struct Config {
    int result;
};
int main() {
    Config c;
    c.result = 3;
    std::cout << c.result << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o fix_member fix_member.cpp
$ ./fix_member
3

The output is 3. When the member name has to stay close to an external field name - a serialized field really is called "return" - keep the external spelling in the string that names the field for the serializer, and give the C++ member its own legal name; the mapping between the two is one line in the conversion code, and it is the only place the keyword spelling needs to exist. What does not work is reaching the member through a macro or an alias: a macro that expands to the member name expands to the keyword token, which lands in the third family's problem instead.

The third failure: a macro expanding into a declaration

A program that defines a short macro for a limit, then declares a variable with the same word:

C++
#define count 42
int main() {
    int count = 5;
    return count;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o fail_macro.o fail_macro.cpp
fail_macro.cpp: In function ‘int main()’:
fail_macro.cpp:1:15: error: expected unqualified-id before numeric constant
    1 | #define count 42
      |               ^~
fail_macro.cpp:3:9: note: in expansion of macro ‘count’
    3 |     int count = 5;
      |         ^~~~~

Nothing on line 3 looks wrong, and that is the signature of this family. Object-like macros replace every occurrence of the macro name with the replacement list, before the parser runs. The declaration the parser actually received was int 42 = 5; - the number 42 sitting in the name slot - so the error names a numeric constant, and the error location is the #define line, where that constant lives, with a note pointing back to the declaration that triggered the expansion. Function-like macros do the same thing in function declarations: a #define value() 10 above a definition of value() expands the function's name into 10 and reports the same sentence. The macro does not have to be in this file - a name defined in an included header expands exactly the same way, which is why a declaration can break when an include is added above it.

Separate the two names. The convention that prevents this family is to keep macro names in a shape ordinary identifiers never take - all capitals - and rename one side or the other:

C++
#include <iostream>
#define LIMIT 42
int main() {
    int count = 5;
    std::cout << count << " " << LIMIT << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o fix_macro fix_macro.cpp
$ ./fix_macro
5 42

The output is 5 42 - the variable and the macro both survive once their names differ. The other fix, when the macro is the one that must go, is #undef count before the declaration, which cancels the macro definition from that point on; that is the right repair when a header defined the name and the variable is the established one. Either way, read this family's error backwards: start from the note to find the declaration, then from the error line to find the macro that fed it.

The fourth failure: the name left out of a declaration

A declaration written with a type and an initializer, but no name between them:

C++
int main() {
    int = 5;
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o fail_noname.o fail_noname.cpp
fail_noname.cpp: In function ‘int main()’:
fail_noname.cpp:2:9: error: expected unqualified-id before ‘=’ token
    2 |     int = 5;
      |         ^

The token named in the message is the tell: = cannot be a name, and it is not being accused of trying to be one. The parser consumed int, looked for the name, and the next token was the initializer's =. The name slot is empty. This shape appears when a declaration is edited down from a longer one and the name is deleted with the rest, when a placeholder is left in code that was meant to be filled in, and in declarations generated from a table where one row's name field came out blank. The same emptiness in a function declaration, void () { }, reports "expected unqualified-id before ')' token" - again the token after the missing name, not the name itself, because there is no name to point at. Pointer and reference versions, int* = &value; and int& = value;, report the identical '=' token wording.

Give the declaration its name back:

C++
#include <iostream>
int main() {
    int total = 5;
    std::cout << total << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o fix_noname fix_noname.cpp
$ ./fix_noname
5

The output is 5. If the value genuinely needs no name - a computation performed only for its side effect - the repair is not a nameless declaration but a plain expression statement: call the function, or cast the value to void, and let there be no declaration at all. A declaration without a name declares nothing; the compiler is reporting a hole, and the choice is to fill it with the intended name or to remove the declaration shape entirely.

The fifth failure: a number where the name belongs

A variable named with a leading digit, in the style of a label from a form or a part number:

C++
int main() {
    int 42x = 5;
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o fail_literal.o fail_literal.cpp
fail_literal.cpp: In function ‘int main()’:
fail_literal.cpp:2:9: error: expected unqualified-id before numeric constant
    2 |     int 42x = 5;
      |         ^~~

An identifier's first character must be a letter or an underscore; digits are allowed only after that first character. 42x therefore cannot be read as a name at all - the lexer takes it as a numeric constant (with a suffix it does not recognize), and the parser finds that number standing in the name slot. The wording is the same "numeric constant" as the macro family, and the two are told apart by location: here the error points at the declaration line itself and there is no macro note, because the number is written in the source. A string literal in the slot behaves the same way - int "count" = 5; reports "expected unqualified-id before string constant" - because a literal of any kind is a value, and the slot wants a name.

Move the digit out of the first position. Letters first, digits after, is a legal identifier:

C++
#include <iostream>
int main() {
    int x42 = 5;
    std::cout << x42 << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o fix_literal fix_literal.cpp
$ ./fix_literal
5

The output is 5. When the digit-first label has to survive because it names something outside the program - a form field, a column, a part - the same rule as the member family applies: keep the external spelling in a string, and let the variable carry a legal name that starts with a letter or an underscore, such as field_42x. An underscore alone is a legal first character, so _42x also compiles, but a leading underscore at namespace scope is reserved, and the letter-prefixed form is the one that is safe everywhere.

The sixth failure: nothing after the scope resolution operator

An expression that qualifies a name with its namespace, and stops before the name:

C++
namespace app {
    int value = 7;
}
int main() {
    int x = app::;
    return x;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o fail_scope.o fail_scope.cpp
fail_scope.cpp: In function ‘int main()’:
fail_scope.cpp:5:18: error: expected unqualified-id before ‘;’ token
    5 |     int x = app::;
      |                  ^

A qualified name is the qualifier and an unqualified-id: the name on the right-hand side of :: is looked up in the scope the left-hand side names. app:: promises a name from namespace app and then ends, so the parser, waiting for that name, meets the statement's ; instead. In real code this is almost always an unfinished edit - the qualification was written first and the member name never filled in, or a rename deleted the member and left the qualifier - rather than a considered expression. It also appears when a line is truncated at the :: by a bad merge, which is worth checking before assuming the name was never written: the other side of the merge may hold the complete line.

Write the name the qualifier was reaching for:

C++
#include <iostream>
namespace app {
    int value = 7;
}
int main() {
    int x = app::value;
    std::cout << x << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o fix_scope fix_scope.cpp
$ ./fix_scope
7

The output is 7. If the intended name does not exist in the namespace - the member was renamed or removed - the completed expression reports a different error, that the name "is not a member of" the namespace, and that message, unlike this one, is about lookup rather than syntax: this family means no name was supplied at all, the other means a name was supplied and not found. Telling them apart saves a search through the namespace's declarations for a name that was never typed.

The fixes in reach order

  • The token after "before" is a quoted keyword ('new', 'return', 'this', 'delete'): a reserved word is in a name slot. Rename the variable, member, or function - a longer identifier that merely contains the keyword, such as new_count, is legal.
  • The token is "numeric constant" and the error line is a #define, with an "in expansion of macro" note: a macro replaced the declared name. Rename the macro or the variable so they differ, or #undef the macro before the declaration.
  • The token is "numeric constant" or "string constant" on the declaration line itself, with no macro note: a literal is written where the name belongs. Rename so the identifier starts with a letter or underscore; keep digit-first external labels in strings.
  • The token is a punctuation token ('=', ';', ')'): the name is missing altogether. Restore the intended name, complete the qualified name after ::, or - if no name is needed - turn the declaration into a plain expression statement.
  • The error is inside a class definition: fix the member's name at its declaration; every access written with the keyword spelling reports the same sentence until the declaration is renamed.
  • The wording is anything else - "primary-expression", "identifier", "id-expression", "',' or '...'", "';' before": it is a neighboring problem, not this one. See the traps before renaming anything.

Traps

  • A keyword doing its own job is legal, and identifiers that merely contain a keyword are legal. new allocates, delete frees, and newer is an ordinary name - all in one program:
C++
#include <iostream>
int main() {
    int* p = new int(5);
    int newer = *p + 1;
    std::cout << newer << "\n";
    delete p;
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o trap_legal trap_legal.cpp
$ ./trap_legal
6
Trap
The output is 6. The ban is on the keyword token itself as a name, not on the letters appearing inside a longer identifier or the keyword appearing in its own role. When a declaration fails near a keyword, check whether the keyword is standing alone in the name slot before renaming anything around it. - class as a variable name does not produce this guide's sentence. Because class can begin a declaration of its own, the parser takes a different path and reports a primary-expression error at the type:
C++
int main() {
    int class = 3;
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o trap_class.o trap_class.cpp
trap_class.cpp: In function ‘int main()’:
trap_class.cpp:2:5: error: expected primary-expression before ‘int’
    2 |     int class = 3;
      |     ^~~

The repair is the same rename as the first family, but the wording is not: not every keyword in a name slot reports "unqualified-id", because some keywords redirect the parse before the slot is reached. If a rename-shaped problem does not match this guide's sentence, check what the keyword does in the grammar - a type-introducing keyword like class is the common case. - A keyword as a function parameter name reports yet another wording, because a parameter list is parsed as a list, and the parser stops at the separator it expected:

C++
void show(int new) {
}
int main() {
    show(3);
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o trap_param.o trap_param.cpp
trap_param.cpp:1:15: error: expected ‘,’ or ‘...’ before ‘new’
    1 | void show(int new) {
      |               ^~~

Same disease - a keyword offered as a name - different sentence, because the slot is inside a parameter list rather than a plain declaration. The fix is the same rename, applied to the parameter in the definition and at any declaration of the function. Treat this as a reminder that the exact wording follows the grammatical slot, not the underlying mistake. - A literal after :: reports "id-expression", the close cousin of this guide's wording, not "unqualified-id":

C++
int main() {
    int x = ::5;
    return x;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o trap_scope_literal.o trap_scope_literal.cpp
trap_scope_literal.cpp: In function ‘int main()’:
trap_scope_literal.cpp:2:15: error: expected id-expression before numeric constant
    2 |     int x = ::5;
      |               ^

Compare the sixth family: app:: with nothing after it is "unqualified-id before ';' token", while ::5 supplies a token after the operator and that token is judged as an expression, where the grammar's term is id-expression. The distinction is whether anything follows the :: at all. Both are fixed by putting a real name after the operator; only the first is this guide's error. - A missing value reports "primary-expression", because the slot after = is an expression slot, not a name slot:

C++
int main() {
    int x = ;
    return x;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o trap_primary.o trap_primary.cpp
trap_primary.cpp: In function ‘int main()’:
trap_primary.cpp:2:13: error: expected primary-expression before ‘;’ token
    2 |     int x = ;
      |             ^

This is the mirror image of the fourth family: there the name was missing and the message named the =; here the name is present and the value is missing, and the message names the ;. The token named in each message is the one after the empty slot, which is how the two are told apart - find which slot, name or value, stands immediately before the named token. - A missing semicolon reports "expected ';' before", with the parser supplying the semicolon it wanted in the diagram:

C++
int main() {
    int x = 5;
    return x
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o trap_semi.o trap_semi.cpp
trap_semi.cpp: In function ‘int main()’:
trap_semi.cpp:3:13: error: expected ‘;’ before ‘}’ token
    3 |     return x
      |             ^
      |             ;
    4 | }
      | ~            

Nothing here is a naming problem: every name in the program is legal and declared. The line number in the message is where the parser gave up, one line past the actual omission - with a missing semicolon, the fix belongs at the end of the line above the one the error names.

Checklist

  • The token after "before" in the message has been identified, and classified: keyword, numeric or string constant, or a punctuation token.
  • For a keyword: the name has been renamed everywhere - declaration, definition, and every use or member access - to an identifier that is not on the keyword list.
  • For "numeric constant": the error location has been checked - a #define line means a macro expansion (rename a side, or #undef), the declaration line itself means a literal written in the slot (rename to start with a letter or underscore).
  • For a punctuation token: the missing name has been restored, or the declaration has been rewritten as a plain expression statement where no name was ever needed.
  • For a qualified name: a real member name follows every ::, and if the completed name then reports "not a member", that has been treated as the separate lookup problem it is.
  • Digit-first or keyword external labels that must survive live in strings, with legal C++ names on the variables and members themselves.
  • The wording has been checked against the traps: "primary-expression", "identifier", "id-expression", "',' or '...'", and "';' before" have been treated as their own problems, not renamed blindly.
  • A legal shape - a keyword in its own role, an identifier containing a keyword, a digit after the first character - has been left alone.
  • The fixed program run, and its output checked against what the program was meant to do.

Sources

  • cppreference, Identifiers - for what an identifier is, its first character not being a digit, the entities identifiers name, the exception that identifiers that are keywords cannot be used for other purposes, and unqualified and qualified identifier expressions.
  • cppreference, C++ keywords - for the closed list of reserved keywords, and keywords not being available for re-definition or overloading.
  • cppreference, Replacing text macros - for object-like macros replacing every occurrence of the macro name with the replacement list before parsing, and for #undef canceling a macro definition.
  • cppreference, Qualified name lookup - for a qualified name being the name on the right-hand side of the scope resolution operator, looked up in the scope named on the left-hand side.

Keep Exploring