C2065 C/C++ Origin: Application & API Last updated 15 Oct 2026

MSVC: error C2065: 'x': undeclared identifier

Visual C++ met a name it has not seen declared — a typo, a missing header or namespace, or use outside its scope.

Exact message you may see C2065: undeclared identifiererror C3861: identifier not founderror C2039 is not a member oferror C2143: syntax error: missing ';' before
Seen on: C/C++ MSVC Windows

Meaning

This is MSVC's counterpart of GCC's "was not declared in this scope". With precompiled headers a missing #include "pch.h" first line, a header only included in some configurations, and a wrong _MSC_VER or #ifdef guard are extra causes. Often followed by C3861 ("identifier not found") for functions.

Common causes

  • Missing #include of the header that declares the name
  • Missing std:: qualifier or using declaration
  • Typo or wrong case in the name
  • Variable declared in an inner scope or later in the file
  • #include "pch.h" missing as the first line of a .cpp in a project with precompiled headers

⚡ Quick fix

  1. Include the right header and qualify the name
  2. Put #include "pch.h" first in every .cpp if the project uses precompiled headers
  3. Check spelling and scope

Detailed fix by platform

Visual Studio

  1. Hover the red squiggle → Quick Actions suggests the missing include
  2. In Project Properties → C/C++ → Precompiled Headers, check "Use (/Yu)" and the header name

Windows

  1. For Windows API names such as CreateFile, include <windows.h> and define WIN32_LEAN_AND_MEAN if you want a smaller header set

Code examples

Missing header and namespace

cpp
#include <string>
#include <iostream>        // without this: error C2065: 'cout': undeclared identifier

int main() {
    std::string name = "Ada";
    std::cout << name << "\n";
}

How to diagnose

  1. Header — Which header declares it?
  2. Scope — Used where it is visible?
  3. PCH — Precompiled header include first?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

AddressSanitizer: heap-buffer-overflow — C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N (C/C++)AddressSanitizer: hea…AddressSanitizer: heap-use-after-free — C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free (C/C++)AddressSanitizer: hea…C4996 — MSVC: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead (C/C++)C4996call of overloaded is ambiguous — C++: error: call of overloaded 'f(int)' is ambiguous (C/C++)call of overloaded is…cannot declare variable to be of abstract type — C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions) (C/C++)cannot declare variab…LNK1104 — MSVC: fatal error LNK1104: cannot open file 'foo.lib' (or 'app.exe' while it is running) (C/C++)LNK1104was not declared in this scope — C++: error: 'x' was not declared in this scope (undeclared identifier) (C/C++)was not declared in t…C2065

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.