Skip to content

Improve example for C2061 #4739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions docs/error-messages/compiler-errors-1/compiler-error-c2061.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,54 @@ The compiler found an identifier where it wasn't expected. Make sure that `ident

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

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.
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:

The following sample generates C2061:

```cpp
// C2061.cpp
// compile with: /c
template < A a > // C2061
// try the following line instead
// template < typename b >
class c{};
// compile with: /std:c++17

template <A a> // C2061
class C1 {};

template <typename A a> // ok
class C2 {};

template <typename T>
class C3
{
// Both are valid since C++20
using Type1 = T::Type; // C2061
using Type2 = typename T::Type; // OK
};

int main()
{
int x;
unsigned a1 = alignof(x); // C2061
unsigned a2 = alignof(int); // OK
unsigned a3 = alignof(decltype(x)); // OK
}
```

To resolve the error with `template<A a> class C1{};`, use `template <typename a> class C1 {};`\
To resolve the issue with `using Type1 = T::Type;`, use `using Type1 = typename T::Type;`\
To resolve the issue with `alignof(x)`, replace the argument with the type of `x`. In this case, `int` or `decltype(x);`

C2061 can occur if you pass an instance name to [typeid](../../extensions/typeid-cpp-component-extensions.md):

```cpp
// C2061b.cpp
// compile with: /clr
ref struct G {
ref struct G
{
int i;
};

int main() {
G ^ pG = gcnew G;
int main()
{
G ^pG = gcnew G;
System::Type ^ pType = typeid<pG>; // C2061
System::Type ^ pType2 = typeid<G>; // OK
}
Expand Down