Skip to content

Commit 4746686

Browse files
authored
Update compiler-error-c2061.md
It wasn't as clear what the fix using typename was. You can sort it out, but I decided to make it more explicit for those reading quickly.
1 parent 87e490f commit 4746686

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,53 @@ 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
2323
// compile with: /std:c++17
24-
template <A a> // C2061
25-
// Replace identifier `A` with `typename`:
26-
// template <typename a>
24+
25+
template <A a> // C2061
2726
class C1 {};
2827

28+
template <typename A a> // ok
29+
class C2 {};
30+
2931
template <typename T>
30-
class C2 {
32+
class C3
33+
{
3134
// Both are valid since C++20
32-
using Type1 = T::Type; // C2061
33-
using Type2 = typename T::Type; // OK
35+
using Type1 = T::Type; // C2061
36+
using Type2 = typename T::Type; // OK
3437
};
3538

36-
int main() {
39+
int main()
40+
{
3741
int x;
3842
unsigned a1 = alignof(x); // C2061
39-
unsigned a2 = alignof(int); // OK
40-
unsigned a3 = alignof(decltype(x)); // OK
43+
unsigned a2 = alignof(int); // OK
44+
unsigned a3 = alignof(decltype(x)); // OK
4145
}
4246
```
4347

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+
4451
C2061 can occur if you pass an instance name to [typeid](../../extensions/typeid-cpp-component-extensions.md):
4552

4653
```cpp
4754
// C2061b.cpp
4855
// compile with: /clr
49-
ref struct G {
56+
ref struct G
57+
{
5058
int i;
5159
};
5260

53-
int main() {
54-
G ^ pG = gcnew G;
61+
int main()
62+
{
63+
G ^pG = gcnew G;
5564
System::Type ^ pType = typeid<pG>; // C2061
5665
System::Type ^ pType2 = typeid<G>; // OK
5766
}

0 commit comments

Comments
 (0)