Skip to content

Commit b0f23bf

Browse files
authored
Merge pull request #4739 from Rageking8/improve-example-for-c2061
Improve example for C2061
2 parents 6fd0ca3 + 585e03f commit b0f23bf

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

docs/error-messages/compiler-errors-1/compiler-error-c2061.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,54 @@ The compiler found an identifier where it wasn't expected. Make sure that `ident
1414

1515
An initializer may be enclosed by parentheses. To avoid this problem, enclose the declarator in parentheses or make it a **`typedef`**.
1616

17-
This error could also be caused when the compiler detects an expression as a class template argument; use [typename](../../cpp/typename.md) to tell the compiler it is a type.
17+
This error could also be caused when the compiler detects an expression as a class template argument; use [typename](../../cpp/typename.md) to tell the compiler it is a type, as shown in the following example:
1818

1919
The following sample generates C2061:
2020

2121
```cpp
2222
// C2061.cpp
23-
// compile with: /c
24-
template < A a > // C2061
25-
// try the following line instead
26-
// template < typename b >
27-
class c{};
23+
// compile with: /std:c++17
24+
25+
template <A a> // C2061
26+
class C1 {};
27+
28+
template <typename A a> // ok
29+
class C2 {};
30+
31+
template <typename T>
32+
class C3
33+
{
34+
// Both are valid since C++20
35+
using Type1 = T::Type; // C2061
36+
using Type2 = typename T::Type; // OK
37+
};
38+
39+
int main()
40+
{
41+
int x;
42+
unsigned a1 = alignof(x); // C2061
43+
unsigned a2 = alignof(int); // OK
44+
unsigned a3 = alignof(decltype(x)); // OK
45+
}
2846
```
2947

48+
To resolve the error with `template<A a> class C1{};`, use `template <typename a> class C1 {};`\
49+
To resolve the issue with `using Type1 = T::Type;`, use `using Type1 = typename T::Type;`\
50+
To resolve the issue with `alignof(x)`, replace the argument with the type of `x`. In this case, `int` or `decltype(x);`
51+
3052
C2061 can occur if you pass an instance name to [typeid](../../extensions/typeid-cpp-component-extensions.md):
3153

3254
```cpp
3355
// C2061b.cpp
3456
// compile with: /clr
35-
ref struct G {
57+
ref struct G
58+
{
3659
int i;
3760
};
3861

39-
int main() {
40-
G ^ pG = gcnew G;
62+
int main()
63+
{
64+
G ^pG = gcnew G;
4165
System::Type ^ pType = typeid<pG>; // C2061
4266
System::Type ^ pType2 = typeid<G>; // OK
4367
}

0 commit comments

Comments
 (0)