2012-04-13 56 views
3

考慮以下代碼GCC錯誤,而使用代碼塊const的無符號長

class Reflect : public flemax::annotation::XAnnotation { 
    public: 
    Reflect(const unsigned long id, const std::string& home, const char type, const std::string& name = "me", const int value = 4, const bool valid = false, const signed char gender = 'M') : id_(id), home_(home), type_(type), name_(name), value_(value), valid_(valid), gender_(gender){} 
    ~Reflect() {} 

    const unsigned long id() { return id_; } 
    const std::string& home() { return home_; } 
    const char type() { return type_; } 
    const std::string& name() { return name_; } 
    const int value() { return value_; } 
    const bool valid() { return valid_; } 
    const signed char gender() { return gender_; } 

    private: 
    const unsigned long id_; 
    const std::string home_; 
    const char type_; 
    const std::string name_; 
    const int value_; 
    const bool valid_; 
    const signed char gender_; 

}; // class Reflect 

它不會編譯,編譯器給了我這個奇怪的錯誤。

||=== flemax_base, DebugAnnotator ===| 
/programming/cpp-projects/flemax/flemax_base/base/xannottest.cc|21|error: expected ‘,’ or ‘...’ before ‘long’| 
/programming/cpp-projects/flemax/flemax_base/base/xannottest.cc|24|error: expected ‘;’ before ‘long’| 
/programming/cpp-projects/flemax/flemax_base/base/xannottest.cc|25|error: expected ‘;’ before ‘const’| 
/programming/cpp-projects/flemax/flemax_base/base/xannottest.cc|33|error: expected ‘;’ before ‘long’| 
/programming/cpp-projects/flemax/flemax_base/base/xannottest.cc||In constructor ‘flemax::test::Reflect::Reflect(int)’:| 
/programming/cpp-projects/flemax/flemax_base/base/xannottest.cc|21|error: class ‘flemax::test::Reflect’ does not have any field named ‘id_’| 
/programming/cpp-projects/flemax/flemax_base/base/xannottest.cc|21|error: ‘id’ was not declared in this scope| 
/programming/cpp-projects/flemax/flemax_base/base/xannottest.cc|21|error: argument of type ‘const int (flemax::test::Reflect::)()’ does not match ‘const int’| 
/programming/cpp-projects/flemax/flemax_base/base/xannottest.cc|21|error: argument of type ‘const bool (flemax::test::Reflect::)()’ does not match ‘const bool’| 
/programming/cpp-projects/flemax/flemax_base/base/xannottest.cc|21|error: argument of type ‘const signed char (flemax::test::Reflect::)()’ does not match ‘const signed char’| 
||=== Build finished: 12 errors, 0 warnings ===| 

當我刪除無符號修飾符,一切工作正常。 也許我一直在編碼過去24小時,所以我沒有看到什麼是錯的,並且被打了。 我不想睡覺,直到代碼編譯它的方式。

是在Ubuntu和gcc 4.4.3

由於男性

回答

3

我敢打賭,你有一個#define unsigned WHATEVER某處。或者,也許你正在編寫-Dunsigned=WHATEVER

所以編譯器在任何地方都會看到const WHATEVER long x,而long類型在這裏沒有任何意義。

+0

坦率地說,這樣一個想法並沒有把我的思想當作一個bug。謝謝!! – maress 2012-04-13 20:32:17

+0

現在我想知道的是,宏是如何被首先定義的。 – rodrigo 2012-04-13 20:34:25

+0

代碼正在生成,所以我不能告訴什麼類型的宏定義的類型。這是一個測試生成的代碼,看看它是否編譯 – maress 2012-04-13 20:37:47

0

此代碼編譯我的電腦上,使用gcc 4.6.3使用代碼塊。解決方案是將編譯器版本更改爲最新版本。

但是,這是一個奇怪的錯誤,可能是一個編譯器錯誤。如果您不想更改您的編譯器版本,請嘗試將私有部分放在課程開始處。希望有所幫助。

+0

「Compilator」?當然你的意思是「編譯器」。 – DCoder 2012-04-13 20:00:28

+0

對不起,這是一個正確的詞語在波蘭。 – 2012-04-13 20:01:19

+0

更改爲先私人部分無助於更新到gcc/g ++ 4.6.2 – maress 2012-04-13 20:26:36