编译器错误 C2061

语法错误: 标识符“identifier”

编译器找到了一个意外标识符。 请确保 identifier 在使用之前已声明。

初始值设定项可能由括号括起来。 若要避免此问题,请将声明符括在括号中或让其成为 typedef

当编译器将表达式检测为类模板参数时,也可能导致此错误;使用 typename 告知编译器它是一种类型,如以下示例所示:

下面的示例生成 C2061:

// C2061.cpp
// 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
}

若要解决 template<A a> class C1{}; 错误,请使用 template <typename a> class C1 {};
若要解决 using Type1 = T::Type; 问题,请使用 using Type1 = typename T::Type;
若要解决 alignof(x) 问题,请将参数替换为类型 x。 在本例中为 intdecltype(x);

如果将实例名称传递给 typeid,则可能会出现 C2061:

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

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