Share via


編譯器錯誤 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
}