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.

Syntax Error in C++

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.

Similar Reads

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:...

How to fix the Expected Unqualified Id Error in C++?

...

FAQs

...

Contact Us