共用方式為


編譯器錯誤 C2059

語法錯誤: 'token'

權杖造成語法錯誤。

下列範例會產生宣告 之行 j 的錯誤訊息。

// C2059e.cpp
// compile with: /c
// C2143 expected
// Error caused by the incorrect use of '*'.
   int j*; // C2059

若要判斷錯誤的原因,不僅檢查錯誤訊息中所列的行,也會檢查其上方的行。 如果檢查行不會產生問題線索,請嘗試批註錯誤訊息中所列的行,以及其上方的幾行。

如果在緊接 typedef 變數的符號上發生錯誤訊息,請確定變數是在原始程式碼中定義。

當預處理器符號名稱重新作為識別碼時,會引發 C2059。 在下列範例中,編譯器會視為 DIGITS.ONE 數位 1,這在列舉專案名稱中無效:

#define ONE 1

enum class DIGITS {
    ZERO,
    ONE // error C2059
};

如果符號評估為無任何專案,您可能會得到 C2059,就像使用 /D 符號 = 進行編譯時 可能發生的一樣。

// C2059a.cpp
// compile with: /DTEST=
#include <stdio.h>

int main() {
   #ifdef TEST
      printf_s("\nTEST defined %d", TEST);   // C2059
   #else
      printf_s("\nTEST not defined");
   #endif
}

另一個可能發生 C2059 的情況是,當您編譯應用程式,以指定函式預設引數中的結構時。 引數的預設值必須是運算式。 初始化運算式清單,例如,用來初始化結構的初始化運算式不是運算式。 若要解決此問題,請定義建構函式以執行必要的初始化。

下列範例會產生 C2059:

// C2059b.cpp
// compile with: /c
struct ag_type {
   int a;
   float b;
   // Uncomment the following line to resolve.
   // ag_type(int aa, float bb) : a(aa), b(bb) {}
};

void func(ag_type arg = {5, 7.0});   // C2059
void func(ag_type arg = ag_type(5, 7.0));   // OK

C2059 可能發生于格式不正確的演員。

下列範例會產生 C2059:

// C2059c.cpp
// compile with: /clr
using namespace System;
ref class From {};
ref class To : public From {};

int main() {
   From^ refbase = gcnew To();
   To^ refTo = safe_cast<To^>(From^);   // C2059
   To^ refTo2 = safe_cast<To^>(refbase);   // OK
}

如果您嘗試建立包含句點的命名空間名稱,也可能會發生 C2059。

下列範例會產生 C2059:

// C2059d.cpp
// compile with: /c
namespace A.B {}   // C2059

// OK
namespace A  {
   namespace B {}
}

當可限定名稱的運算子時, :: -> . C2059 可能會發生 ,且後面必須加上 關鍵字 template ,如下列範例所示:

template <typename T> struct Allocator {
    template <typename U> struct Rebind {
        typedef Allocator<U> Other;
    };
};

template <typename X, typename AY> struct Container {
    typedef typename AY::Rebind<X>::Other AX; // error C2059
};

根據預設,C++ 假設 AY::Rebind 不是範本;因此下列 < 會解譯成小於符號。 您必須明確地告知編譯器 Rebind 為範本,以便它可以正確地剖析角括號。 若要更正這個錯誤,請在相依類型名稱上使用 template 關鍵字,如下所示:

template <typename T> struct Allocator {
    template <typename U> struct Rebind {
        typedef Allocator<U> Other;
    };
};

template <typename X, typename AY> struct Container {
    typedef typename AY::template Rebind<X>::Other AX; // correct
};