C++

Error: Expected Unqualified Id

In this article, we will discuss an error which is “Expected Unqualified-Id”. This error is very common and if you are a C++ programmer you would also have encountered such an error. The error “Expected Unqualified-Id” occurs because of the wrong syntax. Syntax errors occur when a coder writes a code that does not match the keywords or standards of procedures of a programming language. For example, a language has syntax designed in such a way that you must have to put a semicolon “;” at the end of every statement. The compiler will generate an error if you didn’t include a semicolon. The error thrown in that case will be called a syntax error. There are many other cases also that lead to syntax errors.

To resolve these errors, one has to read his code syntax carefully and thoroughly to check what mistake has happened while typing the code to correct the error. So, we can say that the specific members in a code are not placed in such a way so that they can do their specified tasks. The compiler takes them as unqualified for that part of the code and throws an error. This error also occurs because of unqualified names of members.

They are not placed in any library or namespace and do not qualify in our code. The following could be the causes of the errors:

  1. Namespaces and Scope of resolution operators declared wrongly.
  2. Invalid variables declared
  3. Missing semicolons or wrong placement of them
  4. Wrong or Invalid Syntax for loop writing.
  5. Wrong Constructor Definition

Syntax

This is an error so instead of a specified syntax it has an error message.

error: expected unqualified-id at end of input

This is how you will get the message once you encounter such an error.

Example # 01:

To understand this error more practically, we will be performing an example in which we will create a situation to get this error and after that, we will diagnose the problem and resolve the error. To do so we have written a loop. A “for” loop. In this loop, we are running this loop 5 times. We are asking the system to start from “i=0” and increment by one. This will stop when “i” becomes less than 5. In the loop body, we are printing the value of “i” for every iteration. After that, in the main method, we did nothing but simply returned 0 as per programming standards.

#include <iostream>
using namespace std;
for(int i=0;i<5;i++)
{
    cout<<i<<endl;
}

int main()
{
    return 0;
}

After the unsuccessful execution of code, the system has thrown an error that is “expected unqualified-id before for”. The system has also printed the line number and the line body which is the cause of the error. Now, let’s resolve the error and see what happens.

To resolve the error, we have simply placed the for loop inside the main method. Now, we will execute the code and notice the output.

After we made amendments to our code, we are now able to successfully execute our code. The problem we encountered was that we were trying to write a for-loop outside the main method which is programmatically wrong. We cannot write a for loop outside the main method. But we can write other things like classes, structures, functions, etc. outside the main method. So, this is a pure example of how due to a very minor mistake we can get this error.

Example # 02:

In this example, we will not be doing very complex programming. Instead, we will write a very simple code that will multiply two values and print the output after the operation. For that, we have declared two variables “1a” and “2a”. We have assigned “1” to “1a” and “2” to “2a”. In line 10, we have declared another integer “3a” which will store the result from multiplying “1a” and “2a”. After that, we printed “3a” to check the output, let’s execute our code.

#include <iostream>

using namespace std;
int main()
{
    int 1a = 1;
    int 2a = 2;

    int 3a = 1a*2a;
    cout<<3a<<endl;

    return 0;
}

Once again, we have the same error after we tried to execute our code. But this time we wrote everything according to C++ standards and did not write anything extra or any code outside the main method. Then what is the reason that we have encountered this error again? Let’s analyze the output message and see what the compiler is trying to convey.

The system has thrown the error on lines 7,8, and 10. One thing common in all these lines is that we are declaring our variables in these lines. This means that we are making a very minute mistake while declaring our variables. Now, let’s try to remove the error and see what mistake we are trying to make here.

We simply changed our variables from “1a” to “a1” and “2a” to “a2” and “3a” to “a3”. This was the only mistake we made. Now, if we go back to the introduction for an instance, we discussed there that this error might occur due to an invalid declaration of variables. In C++, it is forbidden to declare variables like “1a”. We can put the number after the alphabet but cannot put it before them. Now, as we have resolved our error, we will execute and we have got our output which is the multiplication of “1” and “2” and the result is.

Conclusion

In this guide, we discussed an error that occurs frequently while programming. This error is “Expected Unqualified-id”. We discussed the causes that lead to this error. We also performed examples to detect and minimize the error. In the end, it is advised from our side that there is no proper mistake to get this error. These types of errors occur only because of bad programming practices or because of weak programming concepts. With the help of examples, we showed you that we did not write any new code but we only restructured and rearranged our existing code and we were successful in resolving our error.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.