3

考虑这段代码

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 ===|

当我删除 unsigned 修饰符时,一切正常。可能我在过去的 24 小时里一直在编码,所以我看不出有什么问题,我很震惊。在该代码按原样编译之前,我不想睡觉。

我在 ubuntu 和 gcc 4.4.3 上使用代码块

谢谢男人

4

2 回答 2

3

我敢打赌,你有一个#define unsigned WHATEVER地方。或者,也许您正在使用-Dunsigned=WHATEVER.

所以编译器const WHATEVER long x到处可见,long类型在那里毫无意义。

于 2012-04-13T20:21:14.713 回答
0

此代码使用 gcc 4.6.3 在我的计算机上编译。解决方案是将编译器版本更改为最新版本。

但是,这是一个奇怪的错误,可能存在编译器错误。如果您不想更改编译器版本,请尝试将私有部分放在类的开头。希望有所帮助。

于 2012-04-13T19:58:35.210 回答