Use of undeclared identifier error comes when compiler can’t find the declaration for an identifier. There are many possible causes for this error. The most common causes are that the identifier hasn’t been declared, the identifier is misspelled, the header where the identifier is declared is not included in the file, or the identifier is missing a scope qualifier. Some common issues and solutions

  • Missing header
    // For 'std::cout' we must include 'iostream' header
    #include <iostream>
    int main() {
        std::cout << "Hello world!" << std::endl;
        return 0;
    }
  • Misspelled variable
    int main() {
      int varName;
      VarName = 1;  /* mind the uppercase V */
      return 0;
    }
  • Incorrect scope
    This error can occur if your identifier is not properly scoped. When C++ Standard Library functions and operators are not fully qualified by namespace, or you have not brought the std namespace into the current scope by using a using directive, the compiler can’t find them. To fix this issue, you must either fully qualify the identifier names, or specify the namespace with the using directive.

    #include <string>
    
    int main() {
      std::string s1 = "Hello"; // Correct.
      string s2 = "world"; // WRONG - would give error.
    }
  • Use before declaration
    void f() { g(); }
    void g() { }

    To fix it, either move the definition of g before f or add a declaration of g before f.

    // Fix 1
    void g() { }
    void f() { g(); }
    
    // Fix 2
    void g();           // Declaration
    void f() { g(); }
    void g() { }        // Definition
  • Missing closing quote
    #include <iostream>
    
    int main() {
      // Fix this issue by adding the closing quote to "Aaaa"
      char *first = "Aaaa, *last = "Zeee";
      std::cout << "Name: " << first
            << " " << last << std::endl;
    }
  • Preprocessor removed declaration
    This error can occur if you refer to a function or variable that is in conditionally compiled code that is not compiled for your current configuration.

    #ifdef _DEBUG
        int condition;
    #endif
    int main() {
    
      std::cout << condition;
      // Fix by guarding references the same way as the declaration:
      // #ifdef _DEBUG
      //        std::cout << condition;
      // #endif
    }