Compiler Error C2059

syntax error : 'token'

The token caused a syntax error.

The following example generates an error message for the line declaring j.

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

To determine the cause of the error, examine not only the line listed in the error message, but also the lines above it. If examining the lines yields no clue to what the problem might be, try commenting out the line listed in the error message and possibly several lines above it.

If the error message occurs on a symbol immediately following a typedef variable, check that the variable is defined in the source code.

You may get C2059 if a symbol evaluates to nothing, as can occur when you compile with /Dsymbol**=**.

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

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

Another specific reason you can get C2059 is when you compile an application that specifies a structure in the default arguments for a function. The default value for an argument must be an expression. An initializer list, such as that used to initialize a structure, is not an expression. The resolution is to define a constructor to perform the required initialization.

The following example generates 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

You can also get C2059 if you define a member template class or function outside the class. For information, see Knowledge Base article 241949.

C2059 can occur for an ill-formed cast.

The following sample generates 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 can also occur if you attempt to create a namespace name that contains a period.

The following sample generates C2059:

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

// OK
namespace A  {
   namespace B {}

C2059 can also occur if you define a member template class or function outside the class. See Knowledge Base article Q241949 for more information. You can find Knowledge Base articles on the MSDN Library CD-ROM or at

Change History




December 2008

Reinstated a version of the C2059e.cpp example code.

Customer feedback.