编译器错误 C2059

语法错误 :“token”

该标记导致语法错误。

下面的示例生成声明 j的行的错误消息。

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

若要确定该错误的原因,请检查错误消息中列出的行,不仅,还要检查该行上面的行。如果检查行不提供有关该问题的主管,请尝试注释掉可能位于错误消息和若干行列表在它上面的行。

如果错误消息在紧靠 typedef 变量的符号时,请确保变量在源代码中定义。

您可能会收到 C2059 符号,则计算结果为 nothing,可以出现问题,则 /Dsymbol**=** 用于生成。

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

// 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。有关信息,请参见 知识库文章 241949

对于格式错误的强制转换,会发生 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
};