2014-05-14 34 views
0

此問題與GCC says "syntax error before numeric constant" in generated header file from bisonI'm getting an error concerning enum (I think)有關,但這裏的答案僅給出了爲什麼人們可能會看到錯誤「error:syntax error before numeric constant」的錯誤原因。除非我掩飾它,否則我沒有看到任何好的解決方案來避免這個問題(當然除了簡單地重命名我們的枚舉常量)。因此,我的問題是:除了簡單地重命名枚舉常量以避免這種命名衝突,還有其他(優選)方法來解決此問題嗎?使用名稱空間似乎不起作用。避免預定義的數字常量與C++中的枚舉相沖突

更新版(命名空間): 我得到這個錯誤:

enum.cpp:5:5: error: expected identifier before numeric constant 
enum.cpp:5:5: error: expected ‘}’ before numeric constant 
enum.cpp:5:5: error: expected unqualified-id before numeric constant 
enum.cpp:7:1: error: expected declaration before ‘}’ token 

該程序:

#include <sys/ioctl.h> 

namespace mine { 
    enum test { 
    NCC 
    }; 
} 

int main(int argc, char** argv) 
{ 
    return 0; 
} 

注意,我得到編譯該程序時同樣的錯誤:

#define NCC 5 

namespace mine { 
    enum test { 
    NCC 
    }; 
} 

int main(int argc, char** argv) 
{ 
    return 0; 
} 
+2

請說明如何應用命名空間,以便我們診斷它。 – wallyk

+3

如果它是與你碰撞的預處理器符號,命名空間將無濟於事。 –

+0

現在更新了名稱空間似乎不起作用的方式。 – t2k32316

回答

0

在C++中,您可以使用名稱空間來保持它們不融合。

+1

從問題:「使用名稱空間似乎不起作用。」 –

1

我知道要做到這一點的唯一方法是取消定義你即將在枚舉重新定義contants /符號:

#include <sys/ioctl.h> 
#undef NCC 

namespace { 
    enum { 
     NCC 
    } 
} 

這編譯。

請記住,我假設你真的想重新定義該符號。如果是這樣,那就是你如何做到的。