What does error C2065 undeclared identifier mean in C++?
Why the compiler stops
C++ requires every name to be declared before use. When the compiler reaches an identifier with no matching declaration in scope, it reports C2065 and names the identifier. The message is raised at compile time, before the program runs.
Typical triggers include a misspelled variable, a variable declared inside a block that has already closed, a function from a header you did not include, or a name from a namespace you did not qualify. The error message usually points to the exact line.
- Check spelling and capitalization
- Confirm the variable is in the same scope
- Include the header that defines the name
How to fix it
Start by reading the first error in the list, since later errors often come from the same mistake. Declare the variable before first use, or move the declaration to an outer scope if other code needs it.
If the name comes from the standard library, include the right header and use the std namespace or a qualified name like std::string. Recompile after each fix so new errors are easier to track.
Missing or circular imports are the most common culprits for this message, so check the imports at the top of the file first. Restarting the editor's language server can also clear stale errors.
Common mistakes
- Fixing the second or third error before the first one.
- Adding using namespace std everywhere without understanding the names it brings in.

Related questions
- What does expected declaration or statement at end of input mean in C?
- How do I fix a missing return statement error in Java?
- What does cannot find package mean in Go?
- Why do I get a duplicate class error in Java?
- What does no matching function for call to mean in C++?
- How do I fix an unresolved external symbol error in C++?