C2086
Visual C++ Programming Language
Severity: MinorWhat Does This Error Mean?
C2086 means an identifier — a variable, type, or function — has been defined more than once in the same scope. The message reads: C2086: 'identifier': redefinition. Missing #pragma once or #ifndef include guards in a header file is the most common cause — the header gets included twice and everything it declares is defined twice.
Affected Models
- Visual Studio 2015–2022
- MSVC v14.x / v17.x
Common Causes
- A header file is missing #pragma once or #ifndef guards, causing its contents to be included multiple times
- The same variable is declared with an initialiser in a header, and then the header is included from multiple .cpp files
- Two different .cpp files both define the same global variable or function without it being declared extern
- A local variable is declared twice in the same block — often a copy/paste mistake
How to Fix It
-
Add #pragma once to the very top of any header file that is missing it. This prevents the file from being processed more than once per translation unit.
#pragma once is supported by all modern compilers including MSVC, GCC, and Clang. Alternatively, use the traditional #ifndef MY_HEADER_H / #define MY_HEADER_H / #endif pattern.
-
Move variable definitions out of header files into .cpp files. In the header, use extern to declare without defining: extern int myGlobal;
A variable defined (int count = 0;) in a header that is included in multiple .cpp files creates multiple definitions — one per translation unit. Only declarations belong in headers; definitions go in exactly one .cpp file.
-
If two .cpp files define the same function, make one of them the authoritative definition and remove the other, or rename one of the functions.
Functions defined in header files have the same problem unless they are marked inline. Marking a function inline allows multiple identical definitions across translation units.
Frequently Asked Questions
What is the difference between a declaration and a definition in C++?
A declaration tells the compiler a name exists: extern int x; or void foo(); A definition provides the actual storage or implementation: int x = 0; or void foo() { }. Declarations can appear many times; definitions must appear exactly once across all translation units (the one-definition rule).
Can C2086 appear for a type (struct or class) as well as a variable?
Yes — if a struct or class definition appears in a header without include guards, and that header is included from two places that both get combined in the same translation unit, C2086 fires for the type. The fix is the same: add #pragma once to the header.