Open In App

Syntax Error in C++

Last Updated : 15 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

FAQs

1. What is Syntax Error?

Syntax Error is a type of error that occurs when rules are violated while writing the syntax during a program.

2. What causes syntax errors?

There are only a few specified causes of syntax errors mentioned below:

  • Wrong semicolons use.
  • Not using quotes properly.
  • Header file not included.
  • Problem while declaring variables.
  • Using more or less a number of Braces.

3. What is syntax vs semantic error?

Syntax errors occur because of the wrong syntax uses whereas Semantic error occurs because of improper use of programming statements.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads