# MSVC: error C2065: 'x': undeclared identifier

**Code:** `C2065` · **Category:** C/C++ · **Seen on:** C/C++, MSVC, Windows

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

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/msvc-c2065-undeclared-identifier)
