Syntax plays a vital role and even a slight mistake can cause a lot of unexpected errors. One of these errors is the "expected unqualified id error" or "expected unqualified id before ' ' token" which can arise due to some common oversights while writing your code. In this article, we are going to dive deep into the expected unqualified id error and what can be its possible solutions.
What is an Expected Unqualified Id Error?
The Expected Unqualified Id error is one of the most commonly encountered errors in C++ programming. It is an error that occurs when the code which is written does not match the standards and rules of the programming language. Also, known as a syntax error.
Why does this error occur?
The expected unqualified id error mainly occurs due to mistakes in the syntax of our C++ code. Some of the most common reasons for this error are as follows:
- Omitted or Misplaced Semicolons
- Writing Strings without Quotes
- Header Files Not Included
- Invalid Variable Declaration
- Under or Over-usage of Braces
1. Omitted or Misplaced Semicolons
This type of error is very typical and may arise when we place the semicolon in the wrong place or if our code misses a semicolon.
C++
// C++ program to demonstrate the misplaced semicolon
#include <iostream>
using namespace std;
class teacher; // wrong semicolon placement
{
private:
string num;
public:
void setNum(int num1) { num = num1; }
string getNum() { return num }
};
int main() { return 0; }
error: expected unqualified-id before '{' token
4 | class teacher;{
|
The above code produced an error. You will notice that the class ‘teacher’ has a semi-colon and the ‘return num’ statement does not. To solve the error you need to remove the semi-colon from the ‘teacher’ class and add a semi-colon at the end of the ‘return’ statement.
2. Writing string values without quotes
Another common mistake that you can make is specifying the values of the string without quotes. C++ does not accept the string value without quotes and interprets it as a variable and throws the ‘expected unqualified id’ error.
C++
// C++ Program to demonstrate the error for using strings
// without quotes
#include <iostream>
using namespace std;
int main()
{
// using string without semicolon
cout << please enter your age << endl;
cin >> age;
}
error: 'please' was not declared in this scope
7 | cout << please enter your age << endl;
|
We have not enclosed ‘please enter your age’ in quotes which is why this piece of code will produce an error.
3. Header File not Included
C++ has a vast amount of libraries defined inside the header file. To use those libraries, we must first include those header files otherwise an expected unqualified error is encountered.
C++
// C++ Program to show the error due to missing header file
// inclusion
int main()
{
cout << "GFG!";
return 0;
}
error: 'cout' was not declared in this scope
3 | cout << "GFG!";
| ^~~~
4. Invalid Variable Declaration
While writing the code, you should be mindful of not declaring your functions with the same keywords reserved by the language.
C++
// C++ Program to show invalid variable declaration
#include <iostream>
using namespace std;
int main()
{
// using keyword as variable name
int case = 10;
cout << case;
return 0;
}
error: expected unqualified-id before 'case'
8 | int case = 10;
In the above example, we used “delete” as our function name which is a reserved keyword. The delete function is an inbuilt function in C++ used to deallocate a class object's memory.
5. Over or Under Usage of Braces
Curly braces in C++ are used to declare various variables and help to determine where the statement begins and where it ends and the scope. The curly braces always come in pairs i.e. opening and closing braces. So if any of them is missing, an error is shown.
C++
// C++ Program to show the effect of missing braces
#include <iostream>
using namespace std;
int main()
{
if (true) {
cout << "You choose the black color";
}
else if (false) {
cout << "You choose the purple color";
// missing brace here
return 0;
}
error: expected '}' at end of input
13 | }
| ^
In the above example, we missed one brace after the if statement which means that the statement is incomplete and will produce the state error.
How to fix the Expected Unqualified Id Error in C++?
Since all of these errors occur due to incorrect syntax, we can avoid these errors by using the correct syntax in our program. Nowadays, many popular code editors contain plugins to check for syntax errors automatically and highlight them even before compilation so it is easy to find and fix these errors.
We can also keep in mind the following points which are one of the most common reasons for this error:
1. Placing Accurate Semi-colons and Braces
Simply placing semi-colons at the end of the statements according to the standards will help in avoiding this error.
C++
#include <iostream>
using namespace std;
int main()
{
int a = 3;
int b = a % 25;
cout << b << endl;
return 0;
}
3
In the code above, we placed a semi-colon after every declaration which helps the compiler to understand that the statement ends here and your code will run successfully.
2. Valid Variable Declaration
You should not declare a variable name that starts with a numeric character as it is not allowed. Also, keywords cannot be used as variables and the identifiers must be unique in their scope so keeping that in mind while declaring the variables will help in avoiding these types of errors.
C++
#include <iostream>
using namespace std;
int main()
{
int abc = 10;
int def = 5;
int ijk = abc * def;
cout << ijk;
return 0;
}
50
The above code is an example of how you can declare variables to avoid these types of errors.
Similar Reads
C++ Basic Syntax Syntax refers to the rules and regulations for writing statements in a programming language. They can also be viewed as the grammatical rules defining the structure of a programming language.The C++ language also has its syntax for the functionalities it provides. Different statements have different
4 min read
goto Statement in C++ The goto statement in C++ is a control flow statement that provides for an unconditional jump to the same function's predefined label statement. In simple terms, the goto is a jump statement transfers the control flow of a program to a labeled statement within the scope of the same function, breakin
5 min read
C++ Pointer Operators Prerequisite: Pointers in C++ A pointer variable is a variable that stores the address of another variable or in other a pointer variable points to the variable whose address is stored inside it. Syntax: int *pointer_name; There are mainly two types of Pointer operators mainly used: Address of opera
2 min read
std::function in C++ The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s
5 min read
C++ Tutorial | Learn C++ Programming C++ is a popular programming language that was developed as an extension of the C programming language to include OOPs programming paradigm. Since then, it has become foundation of many modern technologies like game engines, web browsers, operating systems, financial systems, etc.Features of C++Why
5 min read
Naming Convention in C++ Naming a file or a variable is the first and the very basic step that a programmer takes to write clean codes, where naming has to be appropriate so that for any other programmer it acts as an easy way to read the code. In C++, naming conventions are the set of rules for choosing the valid name for
6 min read
C++ Relational Operators In C++, Relational operators are used to compare two values or expressions, and based on this comparison, it returns a boolean value (either true or false) as the result. Generally false is represented as 0 and true is represented as any non-zero value (mostly 1).Syntax of Relational OperatorsAll C+
3 min read
#define in C++ In C++, #define is a preprocessor directive used to define a macro. Macros are a way to represent a fragment of code or a constant value by giving it a name. When the preprocessor encounters the macro name in the code, it replaces it with the corresponding code fragment or value that is defined usin
4 min read
C++ if else Statement The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false, it wonât. But what if we want to do something else if the condition is false. Here comes the C++ if else statement. We can use the else statement with if statement to exec
3 min read
Compound Statements in C++ Compound statements in C++ are blocks used to group multiple statements together into a single unit. These statements are enclosed in curly braces {} and can be used wherever a single statement is expected. The statements put inside curly braces are executed just like a single statement would have b
3 min read