Skip to content

Commit 00ebb79

Browse files
author
Colin Robertson
authored
Update compiler-error-c2512.md
Expand explanations to address some common confusion. Remove examples that now generate different errors.
1 parent a5916b4 commit 00ebb79

File tree

1 file changed

+27
-71
lines changed

1 file changed

+27
-71
lines changed

docs/error-messages/compiler-errors-2/compiler-error-c2512.md

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -18,74 +18,30 @@ manager: "ghogen"
1818
ms.workload: ["cplusplus"]
1919
---
2020
# Compiler Error C2512
21-
'identifier' : no appropriate default constructor available
22-
23-
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.
24-
25-
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.
26-
27-
The following sample generates C2512 and shows how to fix it:
28-
29-
```
30-
// C2512.cpp
31-
// C2512 expected
32-
struct B {
33-
B (char *);
34-
// Uncomment the following line to fix.
35-
// B() {};
36-
};
37-
38-
int main() {
39-
B b;
40-
}
41-
```
42-
43-
The following sample shows a more subtle C2512. To fix this error, rearrange the code to define the class before its constructor is referenced:
44-
45-
```
46-
// C2512b.cpp
47-
// compile with: /c
48-
struct S {
49-
struct X;
50-
51-
void f() {
52-
X *x = new X(); // C2512 X not defined yet
53-
}
54-
55-
};
56-
57-
struct S::X {};
58-
59-
struct T {
60-
struct X;
61-
void f();
62-
};
63-
64-
struct T::X {};
65-
66-
void T::f() {
67-
X *x = new X();
68-
}
69-
```
70-
71-
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.
72-
73-
The following sample generates C2512 and shows how to fix it:
74-
75-
```
76-
// C2512c.cpp
77-
// Compile by using: cl /c /W3 C2512c.cpp
78-
struct S {
79-
const int i_;
80-
int &r_;
81-
};
82-
83-
int SumS() {
84-
const int ci = 7;
85-
int ir = 42;
86-
87-
S s1; // C2512 - no default constructor available
88-
S s2{ci, ir}; // Fix - initialize const and reference members
89-
return s2.i_ + s2.r_;
90-
}
91-
```
21+
22+
> '*identifier*' : no appropriate default constructor available
23+
24+
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.
25+
26+
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.
27+
28+
## Example
29+
30+
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.
31+
32+
```cpp
33+
// C2512.cpp
34+
// Compile with: cl /W4 c2512.cpp
35+
// C2512 expected
36+
struct B {
37+
B (char *) {}
38+
// Uncomment the following line to fix.
39+
// B() {}
40+
};
41+
42+
int main() {
43+
B b; // C2512 - This requires a default constructor
44+
}
45+
```
46+
47+
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) {}`.

0 commit comments

Comments
 (0)