What is an “undeclared identifier“ in C++?
In C++, the “undeclared identifier” generates due to a mistake in the declaration of the variable or function name, which causes an error during the compilation time.
There are multiple reasons for encountering such errors, and some of the common reasons are discussed below along with their solutions:
- Incorrect Variable Name
- Calling a Variable Out of its Scope
- Calling an Undeclared Variable
- Missing Library
1: Incorrect Variable Name
In a C++ program, the variable name must use the same spelling and format as declared in the program. If we do not use the correct variable name in which it is defined, then it triggers the error of “undeclared identifier”.
The following program simply takes two variables and finds the sum of variable values, but it generates an error as the variable sum is misspelled in the program:
using namespace std;
int main(){
int num1,num2,sum;
cout<<"Enter two numbers:"<<endl;
cin>>num1>>num2;
sun=num1+num2; //misspelling the variable sum cause an error
cout<<"The sum of two number is "<<sum<<endl;
return 0;
}
This code shows an error you can see in the following output as the sum variable is used as sun, which is a mistake in spelling:
How to Fix this Error?
The following code simply uses the same spelling of the sum variable all over the program:
using namespace std;
int main(){
int num1,num2,sum;
cout<<"Enter two numbers:"<<endl;
cin>>num1>>num2;
sum=num1+num2;
cout<<"The sum of two number is "<<sum<<endl;
return 0;
}
Hence, the program executes without any compilation error and displays the result as follows:
2: Calling a Variable Out of its Scope
This type of error occurs when we want to access the variable outside its scope declaration. The variable is only used in the program where it is declared in the scope. Below is the code which generates this type of error:
using namespace std;
int main(){
int num=0;
for(int j=0;j<5;j++){ //j is only declared in the scope of for loop
num++;
}
cout<<"The output is :"<<num*j; //here j is not declared for the main function
return 0;
}
Here in the program j variable is only declared in for loop, but it is called outside the for loop in the main program so, it triggers of scope error:
How to Fix this Error?
If we declare the j variable as in the main function, it can be used anywhere in the program:
using namespace std;
int main(){
int num=0,j=0;
for(j;j<5;j++){
num++;
}
cout<<"The output is: "<<num*j;
return 0;
}
After running this program, the below output will display on the terminal:
3: Calling an Undeclared Variable
Sometimes the error generates in the C++ code, as we forget to declare the name of the variable and use it directly in the code. According to C++ compiler specification, it will generate an error:
using namespace std;
int main(){;
cout<<"Enter a number"<<endl;
cin>>num1;
num1=num1+1;
cout<<"The value of num1 is: "<<num1;
return 0;
}
The above code generates an error as num1 is used in the program without declaring it initially. The following is the screenshot that causes an error while the compilation:
How to Fix this Error?
We simply first declare num1 as int num1, then perform an operation on num1 in the C++ program:
using namespace std;
int main(){
int num1;
cout<<"Enter a number"<<endl;
cin>>num1;
num1=num1+1;
cout<<"The value of num1 is: "<<num1;
return 0;
}
The above program executes successfully takes a number from the user and increments it by one and displays the results:
4: Missing Library
The error of “undeclared identifier” can also occur when we forget to declare the respective library in the program:
int main(){
int num,num1,mul;
cout<<"Enter two numbers:"<<endl;
cin>>num>>num1;
mul=num*num1;
cout<<"The multiplication of two numbers is "<<mul<<endl;
return 0;
}
This code triggers an error on the screen as shown below, as it does not include the <iostream> library:
How to Fix this Error?
The only way to fix this error is to add the library and in this case, it is “<iostream>”:
using namespace std;
int main(){
int num,num1,mul;
cout<<"Enter two numbers:"<<endl;
cin>>num>>num1;
mul=num*num1;
cout<<"The multiplication of two number is "<<mul<<endl;
return 0;
}
When you process this program, it will show the output that multiplies two numbers entered by the user, as shown in the below output:
Conclusion
The error known as an “undeclared identifier” arises when an identifier is not declared correctly according to C++ syntax. This error is mainly encountered because of the four reasons mentioned in this guide, such as incorrect variable name, calling a variable out of its scope, calling an undeclared variable, and missing library.