C++: error: redefinition of 'class X' / multiple definition — missing include guard
The same class, struct or function body was included twice in one translation unit because the header has no include guard.
redefinition of 'classredefinition of 'structerror: redefinition ofprevious definition of 'classconflicting declaration Meaning
When two headers both include shape.h, the compiler sees the class definition twice and reports a redefinition. Add #pragma once or an #ifndef guard to every header, and never define non-inline functions or variables in headers.
Common causes
- Header without
#pragma onceor#ifndefguard - Two guards with the same macro name in different headers
- Including a .cpp file instead of its header
- Function or variable defined (not just declared) in a header
⚡ Quick fix
- Add
#pragma onceas the first line of each header - Use unique guard names like
MYAPP_SHAPE_H - Include headers only, never .cpp files
Detailed fix by platform
C++
- Put
#pragma onceat the top of every header, or wrap it in#ifndef/#define/#endif - Move function bodies to a .cpp file, or mark them
inline
Code examples
Header guard
// shape.h
#pragma once // or: #ifndef APP_SHAPE_H / #define APP_SHAPE_H ... #endif
class Shape {
public:
virtual double area() const = 0;
virtual ~Shape() = default;
};How to diagnose
- Include chain — Is the header reachable through two paths?
- Guard — Does every header have a unique guard?
- Source — Did something #include a .cpp?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- Could not find a package configuration file CMake Error: Could not find a package configuration file provided by "Foo" (requested version 1.2)
- fatal error: No such file or directory C/C++: fatal error: foo.h: No such file or directory — #include not found
- implicit declaration of function C: warning: implicit declaration of function 'foo' [-Wimplicit-function-declaration]
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- expected ';' before C/C++: error: expected ';' before 'x' — missing semicolon
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 15 Oct 2026