diff --git a/docs/error-messages/compiler-errors-2/compiler-error-c2512.md b/docs/error-messages/compiler-errors-2/compiler-error-c2512.md index ca5fba08db0..5af6b9d0cb8 100644 --- a/docs/error-messages/compiler-errors-2/compiler-error-c2512.md +++ b/docs/error-messages/compiler-errors-2/compiler-error-c2512.md @@ -1,7 +1,7 @@ --- title: "Compiler Error C2512 | Microsoft Docs" ms.custom: "" -ms.date: "11/04/2016" +ms.date: "02/09/2018" ms.reviewer: "" ms.suite: "" ms.technology: ["cpp-tools"] @@ -18,74 +18,30 @@ manager: "ghogen" ms.workload: ["cplusplus"] --- # Compiler Error C2512 -'identifier' : no appropriate default constructor available - - No default constructor is available for the specified class, structure, or union. The compiler supplies a default constructor only if user-defined constructors are not provided. - - If you provide a constructor that takes a non-void parameter, and you want to allow your class to be created with no parameters—for example, as the elements of an array—you must also provide a default constructor. The default constructor can be a constructor with default values for all parameters. - - The following sample generates C2512 and shows how to fix it: - -``` -// C2512.cpp -// C2512 expected -struct B { - B (char *); - // Uncomment the following line to fix. - // B() {}; -}; - -int main() { - B b; -} -``` - - The following sample shows a more subtle C2512. To fix this error, rearrange the code to define the class before its constructor is referenced: - -``` -// C2512b.cpp -// compile with: /c -struct S { - struct X; - - void f() { - X *x = new X(); // C2512 X not defined yet - } - -}; - -struct S::X {}; - -struct T { - struct X; - void f(); -}; - -struct T::X {}; - -void T::f() { - X *x = new X(); -} -``` - - C2512 can also be caused by a call to default-construct a class that contains a const or reference data member. These members must be initialized in a constructor initializer list, which prevents the compiler from generating a default constructor. - - The following sample generates C2512 and shows how to fix it: - -``` -// C2512c.cpp -// Compile by using: cl /c /W3 C2512c.cpp -struct S { - const int i_; - int &r_; -}; - -int SumS() { - const int ci = 7; - int ir = 42; - - S s1; // C2512 - no default constructor available - S s2{ci, ir}; // Fix - initialize const and reference members - return s2.i_ + s2.r_; -} -``` \ No newline at end of file + +> '*identifier*' : no appropriate default constructor available + +A *default constructor*, a constructor that requires no arguments, is not available for the specified class, structure, or union. The compiler supplies a default constructor only if no user-defined constructors are provided. + +If you provide a constructor that takes a non-void parameter, and you want to allow your class to be created with no parameters (for example, as the elements of an array), you must also provide a default constructor. The default constructor can be a constructor with default values for all parameters. + +## Example + +A common cause of error C2512 is when you define a class or struct constructor that takes arguments, and then you attempt to declare an instance of your class or struct without any arguments. For example, `struct B` below declares a constructor that requires a `char *` argument, but not one that takes no arguments. In `main`, an instance of B is declared, but no argument is supplied. The compiler generates C2512 because it can't find a default constructor for B. + +```cpp +// C2512.cpp +// Compile with: cl /W4 c2512.cpp +// C2512 expected +struct B { + B (char *) {} + // Uncomment the following line to fix. + // B() {} +}; + +int main() { + B b; // C2512 - This requires a default constructor +} +``` + +You can fix this issue by defining a default constructor for your struct or class, such as `B() {}`, or a constructor where all the arguments have default values, such as `B (char * = nullptr) {}`.