class Person
{
private:
int age;
    static const int height = 10; 
public:
Person();
~Person();
Person(const Person& per);
void print();
};
我把static const int height = 10; 这句改为:static const float height = 10; ,,系统提示:only static const integral data members can be initialized within a class难道把int改为float,,,就不再是静态的了,,谁帮忙解释下

解决方案 »

  1.   

    类的静态数据成员不能在类的声明中初始化
    int Person::height = 10;
      

  2.   

    不是的,我用static const int height = 10;这句,,程序就不会出错
    但是改为static const float height = 10;就出错了
    难道改为float以后就不是静态的了吗??
      

  3.   

    一般做法是不在类的声明中里面初始化静态数据结构。一般做法是
    xx.h
    class CTest
    {
    ...
    private:
       static float m_fTest;
    }xx.cpp
    float CTest::m_fTest = 0.0;
      

  4.   

    个人觉得不提倡这种写法,正确的写法还是按照标准的来吧。我查了一下MSDN,的确有这样的写法,以前都没有注意。不过个人并不以为这是语法的问题,可能是编译器的特定要求吧。
      

  5.   

    晕,你怎么不注意integral 这个单词呢,说的很明白,只有静态的,整数常量才可以在类定义中初始化,你的是float当然就不符合integral的要求了呀