C2535
Visual C++ Programming Language
Severity: MinorWhat Does This Error Mean?
C2535 means a member function is defined a second time — it was already defined, either inside the class body or in a previous include of the same header. The message reads: C2535: 'function': member function already defined or declared. The most common cause is defining a member function both inline inside the class and again outside the class with the ClassName:: scope, or including a header without proper include guards.
Affected Models
- Visual Studio 2015–2022
- MSVC v14.x / v17.x
Common Causes
- A member function is defined inline inside the class body and again outside the class in the same header
- A header file is included multiple times and lacks include guards or #pragma once
- A .cpp file that defines member functions is accidentally included (with #include) instead of compiled separately
- Two different headers both define the same class with the same method body
How to Fix It
-
Check whether the function has two definitions — one inside the class body and one outside with ClassName::method scope. Remove one of them.
If you define a function inside the class body, it is implicitly inline. Defining it again outside the class in the same header creates the duplicate C2535 flags.
-
Add #pragma once (or traditional include guards) to every header file that defines class members. This prevents the header from being processed twice in the same translation unit.
#pragma once is the simplest and is supported by all modern compilers. Traditional guards use #ifndef HEADER_NAME_H / #define HEADER_NAME_H / #endif at the top and bottom.
-
Never include a .cpp file with #include. Source files should be compiled separately and linked, not included as headers.
Including a .cpp causes every function defined in it to appear twice in the translation unit — once from the include and once from the separate compilation. Move shared code to a header or a separate .cpp that is added to the project.
Frequently Asked Questions
Can I have the same function name in two different classes without C2535?
Yes — C2535 only fires when the same class has the same member function defined twice. Two different classes can have identically named methods with no conflict.