Error c2059 синтаксическая ошибка строка

I ran into this compilation error when adding an enum to a project. It turned out that one of the values in the enum definition had a name clash with a preprocessor #define.

The enum looked something like the following:


// my_header.h

enum Type 
{
   kUnknown,
   kValue1,
   kValue2
};

And then elsewhere there was a #define with the following:


// ancient_header.h

#define kUnknown L"Unknown"

Then, in a .cpp somewhere else in the project, both of these headers were included:


// some_file.cpp

#include "ancient_header.h"
#include "my_header.h"

// other code below...


Since the name kUnknown was already #define‘d, when the compiler came to the kUnknown symbol in my enum, it generated an error since the symbol was already used to define a string. This caused the cryptic syntax error: 'string' that I saw.

This was incredibly confusing since everything appears to be correct in the enum definition and compiles just fine on it’s own.

It didn’t help that this was in a very large C++ project, and that the #define was being transitively included in a completely separate compilation unit and was written by someone 15 years ago.

Obviously, the right thing to do from here is rename that terrible #define to something less common than kUnknown, but until then, just renaming the enum value to something else works as a fix, e.g.:


// my_header.h

enum Type 
{
   kSomeOtherSymbolThatIsntDefined,
   kValue1,
   kValue2
};

Anyway, hopefully this answer is helpful for someone else, since the cause of this error stumped me for a good day and a half.

description title ms.date f1_keywords helpviewer_keywords ms.assetid

Learn more about: Compiler Error C2059

Compiler Error C2059

03/26/2019

C2059

C2059

2be4eb39-3f37-4b32-8e8d-75835e07c78a

Compiler Error C2059

syntax error : ‘token’

The token caused a syntax error.

The following example generates an error message for the line that declares 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 that’s listed in the error message, but also the lines above it. If examining the lines yields no clue about the problem, try commenting out the line that’s listed in the error message and perhaps several lines above it.

If the error message occurs on a symbol that immediately follows a typedef variable, make sure that the variable is defined in the source code.

C2059 is raised when a preprocessor symbol name is re-used as an identifier. In the following example, the compiler sees DIGITS.ONE as the number 1, which is not valid as an enum element name:

#define ONE 1

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

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

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

Another case in which C2059 can occur 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—for example, one that used to initialize a structure—is not an expression. To resolve this problem, 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

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 occur when an operator that can qualify a name (::, ->, and .) must be followed by the keyword template, as shown in this example:

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
};

By default, C++ assumes that AY::Rebind isn’t a template; therefore, the following < is interpreted as a less-than sign. You must tell the compiler explicitly that Rebind is a template so that it can correctly parse the angle bracket. To correct this error, use the template keyword on the dependent type’s name, as shown here:

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
};

Solved


When I try to compile this program i keep getting these errors:

(50) : error C2059: syntax error :
‘<=’

(50) : error C2143: syntax error
: missing ‘;’ before ‘{‘

(51) : error
C2059: syntax error : ‘>’

(51) : error
C2143: syntax error : missing ‘;’
before ‘{‘

(62) : error C2059: syntax
error : ‘else’

(62) : error C2143:
syntax error : missing ‘;’ before ‘{‘


#include <iostream>
#include <string>
#include <cassert>
using namespace std;

class income {
private:
    double incm;
    double subtract;
    double taxRate;
    double add;
    char status;
public:
    void setStatus ( char stats ) { status = stats; }
    void setIncm (double in ) { incm = in; }
    void setSubtract ( double sub ) { subtract = sub; }
    void setTaxRate ( double rate ) { taxRate = rate; }
    void setAdd ( double Add ) { add = Add; }

    char getStatus () { return status; }
    double getIncm () { return incm; }
    double getsubtract () { return subtract; }
    double getTaxRate () { return taxRate; }
    double getAdd () { return add; }
    void calcIncome ();
};

//calcIncome
int main () {
    income _new;
    double ajIncome = 0, _incm = 0;
    char status = ' ';
    bool done = false;
    while ( !done ) {
        cout << "Please enter your TAXABLE INCOME:n" << endl;
        cin >> _incm;
        if(cin.fail()) { cin.clear(); }
        if ( _incm <= 0) { cout << "the income must be greater than 0... n" << endl; }
        if ( _incm > 0) { done = true; _new.setIncm(_incm); }
    }

    done = false;
    char stt [2] = " ";
    while ( !done ) {
        cout << "Please declare weather you are filing taxes jointly or single" << "n";
        cout << "t's' = singlent'm' = married" << endl;
        cin >> stt;
        if(cin.fail()) { cin.clear(); }
        if ( status == 's' || status == 'm' ) { done = true; _new.setStatus(stt[0]); }
        //if else { }
    }

    return 0;
};

This is part of a homework assignment so any pointers on bettering my programing would be **great**

Note:I am using Windows 7 with VS express C++ 2008

asked Jan 13, 2010 at 15:44

Wallter's user avatar

WallterWallter

4,2656 gold badges29 silver badges33 bronze badges

3

income is the name of your class. _incm is the name of your variable. Perhaps you meant this (notice the use of _incm not income):

if (_incm <= 0) { cout << "the income must be greater than 0... n" << endl; }
if (_incm > 0) { done = true; _new.setIncm(_incm); }

Frequently you use CamelCase for class names and lowercase for instance variable names. Since C++ is case-sensitive, they wouldn’t conflict each other if they use different case.

answered Jan 13, 2010 at 15:47

Jon-Eric's user avatar

Jon-EricJon-Eric

16.9k9 gold badges65 silver badges96 bronze badges

Your variable is named incom, not income. income refers to a type, so the compiler gets confused and you get a syntax error when you try to compare that type against a value in line 50.

One note for bettering you programming would be to use more distinct variable names to avoid such confusions… ;)

answered Jan 13, 2010 at 15:46

sth's user avatar

sthsth

221k53 gold badges281 silver badges367 bronze badges

1

  • Remove From My Forums
  • Вопрос

  • Hello , gentlemen ! I use vs2008 , for an SDI .
    Could anybody please kindly explain why and what to do with error C2059 here ?
    Thank all beforeheand !

    class CDailyRept : public CObject
    {

    public:

      
        CDailyRept();  
        CDailyRept(CString T_Name ,CString R_Name );

    }

    class CMydocDoc : public CDocument
    {

    public:

     
        CDailyRept Chrng;// OK!

     
        CDailyRept Chrng(_T(«abc»), _T(«defg»));// error C2059: syntax error : ‘string’  ???

    }

Ответы

  • Здравствуйте.

    Вы хотите в объявлении класса создать объект? Объявите указатель на объект, а в конструкторе класса уже создавайте объект:

       CDailyRept* Chrng;
    
    	 CMydocDoc() 
    	 { 
    		 Chrng = new CDailyRept(_T("This is a test"), _T("This is a test"));
    	 }
    

    не забудьте в деструкторе освободить память.

    Или есть другой способ иницализировать объект:

    	 CDailyRept Chrng2;
    	 CMydocDoc() : Chrng2(_T("This is a test"), _T("This is a test"))
    	 { }
    

    Только так


    Для связи [mail]

    • Помечено в качестве ответа

      25 апреля 2011 г. 8:35

  • Здравствуйте.

    Не очень понятно, что Вы имееете в виду ? (не знаю английского)

    // SdiDoc.h : интерфейс класса CSdiDoc
    //
    
    
    #pragma once
    
    #include "DailyRept.h"
    class CSdiDoc : public CDocument
    {
    public:
    	//объявление переменных
    	CString message;
    	//(1)Закоментировано. Будет error C2059: синтаксическая ошибка: строка
    	//CString message1(_T("abc"));
    	int num;
    	//(2)Закоментировано. Будет error C2059: синтаксическая ошибка: константа
    	//int num1(1);
    	CDailyRept Chrng;
    	CDailyRept Chrng1;
    	//(3)Закоментировано. Ваш пример.
    	//CDailyRept Chrng(_T("abc"), _T("defg"));// error C2059: syntax error : 'string' ???
    	//Примеры (1),(2),(3) компилятор воспринимает как попытку объявить функции в классе CSdiDoc
    
    	//Пример объявления функции, возвращающей объект CDailyRept
    	//(написать работающий код функции затруднительно из-за особенностей базового класса CObject)
    	CDailyRept Chrng3(CString s1=_T("абв"),CString s2=_T("гдеж"));
    
    //далее из SdiDoc.cpp
    // Объекты в С++ конструируются до начала выполнения кода программы
    // конструктор по умолчанию (если он есть) вызывается автоматом
    // вызов другого конструктора требуется указывать явно.
    // создание/уничтожение CSdiDoc
    CSdiDoc::CSdiDoc():
    	Chrng(_T("abc"), _T("defg"))
    {
    	// TODO: добавьте код для одноразового вызова конструктора
    	// проверка
    	message.Format(_T("Chrng : %srnChrng1 :%s")
    		,Chrng.OutText(),Chrng1.OutText());
    	AfxMessageBox(message,MB_OK);
    }
    
    //далее пример реализации класса
    //файл DailyRept.h
    #pragma once
    #include "afx.h"
    class CDailyRept :
    	public CObject
    {
    public:
    //Конструктор по умолчанию
    	CDailyRept(void)
    	{
    		s1=_T("По умолчанию 1");
    		s2=_T("По умолчанию 2");
    	}
    //Конструктор с параметрами
    	CDailyRept(CString T_Name ,CString R_Name )
    	{
    		s1=T_Name;
    		s2=R_Name;
    	}
    	virtual ~CDailyRept(void)
    	{
    	}
    //Инициализируемые переменные
    	CString s1;
    	CString s2;
    //Функция для проверки результатов инициализации
    	CString OutText(){return (s1 +_T(", ") + s2);}
    };
    
    
    
    • Помечено в качестве ответа
      Abolmasov Dmitry
      25 апреля 2011 г. 8:35

Я просмотрел другие посты и, честно говоря, я до сих пор не уверен, что является причиной проблемы. Я программирую в Visual Studio и

У меня есть следующий код: (это главный C)

int main(int arc, char **argv) {
struct map mac_ip;
char line[MAX_LINE_LEN];

char *arp_cache = (char*) calloc(20, sizeof(char));   //yes i know the size is wrong - to be changed
char *mac_address = (char*) calloc(17, sizeof(char));
char *ip_address = (char*) calloc(15, sizeof(char));

arp_cache = exec("arp -a", arp_cache);

Он использует следующий код cpp:

#include "arp_piping.h"
extern "C" char *exec(char* cmd, char* arp_cache, FILE* pipe) {
pipe = _popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL) {
strcat(arp_cache, buffer);
}
}
_pclose(pipe);
return arp_cache;
}

С соответствующим заголовочным файлом:

#ifndef ARP_PIPING_H
#define ARP_PIPING_H
#endif

#ifdef __cplusplus
#define EXTERNC extern "C"#else
#define EXTERNC
#endif

#include <stdio.h>
#include <string.h>

extern "C" char *exec(char* cmd, char* arp_cache, FILE* pipe);

#undef EXTERNC

Но я продолжаю получать следующие ошибки:

1>d:arp_protoarp_protoarp_piping.h(14): error C2059: syntax error : 'string'
1>main.c(22): warning C4013: 'exec' undefined; assuming extern returning int
1>main.c(22): warning C4047: '=' : 'char *' differs in levels of indirection from 'int'

Пожалуйста, могу ли я получить некоторую помощь, я смотрел на другие сообщения, касающиеся c2059, но до сих пор не получается

3

Решение

Измени свой exec декларация об использовании EXTERNC макрос, который вы постарались определить.

EXTERNC char *exec(char* cmd, char* arp_cache, FILE* pipe);

2

Другие решения

Я столкнулся с этой ошибкой компиляции при добавлении enum к проекту. Оказалось, что одно из значений в enum определение имело конфликт имени с препроцессором #define,

enum выглядело примерно так:


// my_header.h

enum Type
{
kUnknown,
kValue1,
kValue2
};

А потом в другом месте был #define со следующим:


// ancient_header.h

#define kUnknown L"Unknown"

Затем в .cpp где-то еще в проекте оба заголовка были включены:


// some_file.cpp

#include "ancient_header.h"#include "my_header.h"
// other code below...


Поскольку имя kUnknown уже #defineкогда компилятор пришел к kUnknown символ в моем enum, он сгенерировал ошибку, так как символ уже использовался для определения строки. Это вызвало загадку syntax error: 'string' что я видел.

Это было невероятно запутанным, поскольку в enum определение и компилируется просто отлично.

Это не помогло, что это было в очень большом проекте C ++, и что #define был транзитивно включен в совершенно отдельный блок компиляции и был написан кем-то 15 лет назад.

Очевидно, что правильная вещь отсюда переименовать это ужасное #define к чему-то менее распространенному, чем kUnknown, но до тех пор, просто переименовав enum значение для чего-то другого работает как фикс, например:


// my_header.h

enum Type
{
kSomeOtherSymbolThatIsntDefined,
kValue1,
kValue2
};

В любом случае, надеюсь, этот ответ будет полезен для кого-то еще, поскольку причина этой ошибки поставила меня в тупик на добрых полтора дня.

1

extern «C» используется для указания компилятору сделать его грамматикой языка Си, но вы имеете в виду объявить внешнюю функцию exec. Вы просто объединяетесь с этим. поэтому переписайте ваш код следующим образом в arp_piping.h:

/*extern "C"*/ char *exec(char* cmd, char* arp_cache, FILE* pipe);

и затем префикс extern «C» в файле cpp.
если вы хотите компилировать их с грамматикой C, просто установите в cpp вызов функции exec, поэтому напишите так:

extern "C" {
#include "arp_piping.h"}

0

Понравилась статья? Поделить с друзьями:
  • Error 8 кофемашина jura как исправить ошибку
  • Error 601 battery как убрать ошибку
  • Err connection closed как исправить ошибку яндекс браузер
  • Error 6 prometheus ошибка crossout как исправить
  • Err connection aborted ошибка