Fix a “use of undeclared identifier” compilation error in C++

In this tutorial, we will learn how to fix a “use of undeclared identifier” compilation error in C++. The word identifier is used for the names we give to our variable.

This type of error deals with the cases where we make some mistakes while dealing with the identifiers i.e, this type of error is a compilation error that occurs when we make a mistake while working with the variables and their names. This type of error can occur due to multiple reasons and here we will discuss all those reasons.

How to Fix a “use of undeclared identifier” compilation error in C++

  1. VARIABLE NOT DECLARED: When we are using a variable sometimes we might forget to declare it. We keep on writing the code using the variable without declaring it. To fix this, we simply need to declare the variable before using it. For example:
    Here we are using the variable x without declaring it. This will give us an error. We simply need to declare x before using it to fix the error.

    #include<iostream>
    using namespace std;
    int main(){
        x=x+1;
        cout<<x;
        return 0;
    }
    //this code will give us the error
    //to fix this:
    #include<iostream>
    using namespace std;
    int main(){
        int x=0; //to whichever value we want to intialise
        x=x+1;
        cout<<x;
        return 0;
    }
  2. MISSPELLED VARIABLE NAME:  Sometimes while writing the code we may spell the variable name wrong. This is a common type of error because while typing it is very easy to make a spelling mistake. To fix this, simply check every instance of the variable you have used and make sure that it is spelled correctly. For example:
    Here we have spelled abx as abs and hence it gives us an error. To fix this, we make sure that all the variables are spelled correctly.

    #include<iostream> 
    using namespace std; 
    int main(){ 
        int abx=0;
        abx=abs+1; //spelled abx wrong
        cout<<abx; 
        return 0; 
    }
     //this code will give us the error 
    //to fix this:
    /#include<iostream> 
    using namespace std; 
    int main(){ 
        int abx=0;
        abx=abx+1; 
        cout<<abx; 
        return 0; 
    }
  3. OUT OF SCOPE VARIABLE:  If we try to use a variable out of its scope then also this error occurs as the variable remains undeclared out of its scope. To avoid this make sure that you are using a variable inside its scope only. For example:
    Here, the scope of i is only within the for loop. So, when we try to use i outside the loop it throws an error. To solve it simply declare i outside the loop so that the whole program inside the main function can use it.

    #include<iostream> 
    using namespace std; 
    int main(){ 
        int abx=0; 
        for(int i=0;i<5;i++){
            abx++;      //i is only declared for this scope
        }
        cout<<abx*i; //here i is not declared for the outer function
        return 0;  
    } 
    //this code will give us the error 
    //to fix this:
    
    #include<iostream> 
    using namespace std; 
    int main(){ 
        int abx=0,i=0; //i is declared for the main function
        for(i;i<5;i++){
            abx++;
         } 
        cout<<abx*i;//no error 
        return 0; 
    }
  4. LIBRARY NOT INCLUDED: If we try to use a data type such as vector without including its library we will get this error. To fix this, make sure that you are using an identifier only after including its library. For example, we are here using vector without including its library. This given an error. To solve it, simply include the required libraries beforehand.
    #include<iostream> 
    using namespace std; 
    int main(){ 
        vector<int> abx; //abx is not declared
        for(int i=0;i<5;i++){ 
            abx.push_back(i);
        } 
        cout<<abx[2];
        return 0; 
        } 
    //this code will give us the error 
    //to fix this:
    #include<iostream> 
    #include<vector>
    using namespace std; 
    int main(){ 
        vector<int> abx; //abx is declared 
        for(int i=0;i<5;i++){ 
            abx.push_back(i);
        } 
        cout<<abx[2];
        return 0; 
        }

     

READ MORE:

Errors in C++

Leave a Reply

Your email address will not be published. Required fields are marked *