Compiler diagnostic Visual Studio C++
C2084 function already has a body
The same function is defined twice in one source file, often because a header that defines it is included twice.
Visual Studio property-page paths describe MSBuild projects. For CMake, configure the corresponding target or preset. Menu names can vary by Visual Studio version; use the cited reference for your toolset.
error C2084: function 'int Compute(int)' already has a bodyCauses & corrections
Find the case that matches your build. Each example shows what changes and why.
A header that defines functions has no include guard.
Add
#pragma onceto the header.Problem
// util.h, included twice in main.cppint Compute(int x) { return x; }Correction
// util.h#pragma onceinline int Compute(int x) { return x; }Note 1The guard prevents duplicates in one translation unit; inline permits the header in multiple translation units. A function is defined in the class and again outside.
Keep one definition: either the body in the class, or a declaration in the class and the body in the
.cppfile.Problem
struct Worker { int run() { return 1; } };int Worker::run() { return 2; }Correction
struct Worker { int run(); };int Worker::run() { return 2; }Note 2Keep one function body in the translation unit. VideoFilmed in Visual Studio: this case fixed by hand, then with CppPilot. English narration, with subtitles in English, French, German, Italian and Spanish. Transcript
C2084, function already has a body: fixed by hand, then with CppPilot. Counter::Next is defined in the class, and again below it.
The same function has two bodies: the build fails with C2084. Keep one definition: in the class, replace the body with a declaration.
Build again: Next now has a single body, and the build succeeds. Now the same error, fixed by CppPilot.
After the failed build, select the error in the Error List, and click Fix with CppPilot. Click Fix on the C2084 card.
The AI finds both bodies of Counter::Next, and leaves only the declaration in the class. CppPilot rebuilds the project to check the fix: the error is resolved.
CppPilot explains and fixes C++ build errors in Visual Studio.
A
.cppfile is included.Include headers, never
.cppfiles.Problem
#include "util.cpp"int Compute(int x) { return x; }// util.cpp also defines ComputeCorrection
#include "util.h"// util.h declares Compute; util.cpp defines it.// Remove the second body from this file.Note 3Compile the implementation once rather than including it into another source file.
Sources and scope
Official references
These links document the compiler, build system, or debugger behavior discussed here. Use the version selector on Microsoft Learn for your toolset. Example blocks are focused excerpts; required headers and build settings are stated alongside them.
In your workspace
Investigate C2084 with CppPilot
CppPilot gives the selected AI the diagnostic, relevant code, and project settings for the active configuration. Choose Fix to request a correction, then check the result with a rebuild.
Use your own Codex or Claude account, or optional CppPilot Cloud access. Setup instructions
Visual Studio Marketplace