下面的代码在vc6和vc2008中都无法通过编译,太奇怪了!
class CBluetooth{
protected:
int BluetoothStatus;
public:
static const int NO_ERROR; static const int HARDWARE_NOT_DETECTED;
static const int HARDWARE_RESET_FAILED;
static const int DRIVER_NOT_INSTALLED;
}
const int CBluetooth::NO_ERROR = 0;const int CBluetooth::HARDWARE_NOT_DETECTED = 1;
const int CBluetooth::HARDWARE_RESET_FAILED = 2;
const int CBluetooth::DRIVER_NOT_INSTALLED = 3;错误信息是:
error C2059: syntax error : 'constant'                         main.cpp
error C2238: unexpected token(s) preceding ';'                 main.cpp
error C2589: 'constant' : illegal token on right side of '::' main.cpp

解决方案 »

  1.   

    class test
    {
    public:
    static const int xxx = 1234;
    static int yyy;
    };
    int test::yyy=5678;int main()
    {
    test x;
        return 0;
    }
      

  2.   

    1. class 定义少了分号';'
    2. NO_ERROR 是已经定义的宏
    class CBluetooth{ 
    protected: 
    int BluetoothStatus; 
    public: 
    static const int NO_ERRORS; 
    static const int HARDWARE_NOT_DETECTED; 
    static const int HARDWARE_RESET_FAILED; 
    static const int DRIVER_NOT_INSTALLED; 
    };const int CBluetooth::NO_ERRORS = 0;
    const int CBluetooth::HARDWARE_NOT_DETECTED = 1; 
    const int CBluetooth::HARDWARE_RESET_FAILED = 2; 
    const int CBluetooth::DRIVER_NOT_INSTALLED = 3; 
      

  3.   

    同楼上static时,int可以在声明处初始化,其余要在定义处初始化。
      

  4.   

    只有静态变量才是属于整个类的,可在外面赋值。
    即static const int HARDWARE_NOT_DETECTED; 
    而对于 int BluetoothStatus;应该在构造函数内,或者定义处进行赋值。 
      

  5.   


    const int CBluetooth::NO_ERROR = 0;const int CBluetooth::HARDWARE_NOT_DETECTED = 1; 
    const int CBluetooth::HARDWARE_RESET_FAILED = 2; 
    const int CBluetooth::DRIVER_NOT_INSTALLED = 3; 
    class CBluetooth{ 
    protected: 
    int BluetoothStatus; 
    } 你定义的是const变量当然是没有办法改变值了阿!
    如果定义成const就像我所说的这样定义,如果不是用const定义可以使用楼上的方法
      

  6.   

    知道问题出在哪里了,是静态变量NO_ERROR与常量冲突了,太不凑巧了居然找了一个
    原本就有的常量NO_ERROR当静态变量,唉...我想怎么语法都对还是出错
    改名以后一切正常,谢谢大家!