静态数据成员与静态数据函数默认的问题

Yanger_xy 2009-02-16 09:11:27
#include <iostream>
using namespace std;
class Student
{public:
Student(int,int,int);
void total();
static float average();
private:
int num;
int age;
float score;
static float sum;
static int count;
};

Student::Student(int m,int a,int s)
{num=m;
age=a;
score=s;
}

float Student::sum = 0;//为何将这两行删掉后编译能通过,但不能执行?
int Student::count = 0;//static在默认的情况下就是 0 啊!?

void Student::total()
{
sum+=score;
count++;
}

float Student::average()
{ return(sum/count);
}


int main()
{
Student stud[3]={
Student(1001,18,100),
Student(1002,19,90),
Student(1005,20,80)
};
int n;//输入学生的个数 3
cout<<"please input the number of students:";
cin>>n;
for(int i=0;i<n;i++)
stud[i].total();
cout<<"The average score of "<<n<<" students is "<<stud[0].average()<<endl;
return 0;
}


...全文
166 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
dotdotyy 2009-02-18
  • 打赏
  • 举报
回复
/*想验证一下静态变量static float sum和static int count的默认值是否为0!
但是结果又报错: error C2512: 'Student' : no appropriate default constructor available应该如何修改啊?*/
你太粗心了。
声明对象stu,调用构造函数
Student::Student(int m,int a,int s)
{num=m;
age=a;
score=s;
}
而你的构造函数是带参数的,就是说按你的构造函数,声明对象时应该如下形式:
Student stu(1,2,3);
或者再增加一个默认的构造函数
Student::Student() {}
或者只用一个构造函数
Student::Student(int m=1,int a=2,int s=3)
{num=m;
age=a;
score=s;
}


这一块你再翻翻书啊。

8楼不是哥们,是姐们:)
mir1128 2009-02-18
  • 打赏
  • 举报
回复
编译通过是因为编译选项限制不严格
为了节约调试时间,还是在编译时限制严格些好
Yanger_xy 2009-02-18
  • 打赏
  • 举报
回复
按照8楼哥们的说法,如果我将float Student::sum = 0; 这两行改为 float Student::sum; 就可以为其分配空间了?!
int Student::count = 0; float Student::sum;
但是我在vc6.0上试了一下,又遇到了一个问题!
下面是我修改后的程序:
#include <iostream>
using namespace std;
class Student
{public:
Student(int,int,int);
void total();
static float sum;
static int count;
static float average();
private:
int num;
int age;
float score;

};

Student::Student(int m,int a,int s)
{num=m;
age=a;
score=s;
}

float Student::sum ; //没有对其进行赋值!(想使用其,默认的值 0)
int Student::count;

void Student::total()
{
sum+=score;
count++;
}

float Student::average()
{ return(sum/count);
}


int main()
{
Student stu; //定义一个Student类的对象stu
cout << " " << stu.count << stu.sum << endl;/*想验证一下静态变量static float sum和static int count的默认值是否为0!
但是结果又报错: error C2512: 'Student' : no appropriate default constructor available应该如何修改啊?*/
Student stud[3]={
Student(1001,18,100),
Student(1002,19,90),
Student(1005,20,80)
};
int n;//输入学生的个数 3
cout <<"please input the number of students:";
cin>>n;
for(int i=0;i <n;i++)
stud[i].total();
cout <<"The average score of " <<n <<" students is " <<stud[0].average() <<endl;
return 0;
}
dotdotyy 2009-02-18
  • 打赏
  • 举报
回复
就是说如果删掉了那两句话,编译的时候语法上可能没有错误。
可以这样想,执行时,你声明对象数组的时候,系统要创建对象,此时要调用构造函数初始化对象,即为对象的每个数据成员分配内存空间,并同时赋予初值,你删掉那两句话,相当于,count 和 num这两个成员变量没有初始化啊。
但这两个变量不能在构造函数内初始化,因为被声明为static,为所有Student类对象公用的成员变量,所以要放在全局变量的位置初始化。
pengzhixi 2009-02-18
  • 打赏
  • 举报
回复
类的静态数据成员必须在类外定义。
Yanger_xy 2009-02-18
  • 打赏
  • 举报
回复
似乎明白了一点...
但是还是不太懂...
sxbwelcome 2009-02-18
  • 打赏
  • 举报
回复

因为静态数据成员是属于该类的所有对象,而不是属于某个具体的对象。
所以静态数据成员的初始化应在类外单独进行,而且应在定义对象之前进行
dotdotyy 2009-02-17
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
class Student
{……};

Student::Student(int m,int a,int s)
{……
}
/////////////////////////////////////////
上面是类的声明,只是说明你有了实现时需要的图纸。也就是告诉系统,将来在内存中的所有Student类的对象公用两个变量:float型的sum以及int型的count;注意这是公用的,通过类名就可以访问,而诸如num/age/score等是每个对象各有一份,是属于各自管理的,那么只能通过对象名访问。但仅仅是设计说明。

一旦运行,那么就要按照设计说明分配内存空间。
float Student::sum = 0; //或 float Student::sum;
int Student::count = 0; //或 float Student::sum;
这两句就是为公用区分配内存空间,对于静态变量,在vc6.0环境是为其分配空间的同时,赋上0值。

所以是不能删掉的,删掉了,就没有分配内存的动作,这两个变量就无法存在,也就是说系统未能建立起来你数组的三个student对象,按照设计说明少了num和count公共区,初始化不完全,自然不能执行了。
arong1234 2009-02-16
  • 打赏
  • 举报
回复
删掉能编译通过么?以前可不能编译通过的
帅得不敢出门 2009-02-16
  • 打赏
  • 举报
回复
类的static数据成员必须在定义体的外部定义.

float Student::sum = 0;//为何将这两行删掉后编译能通过,但不能执行?
int Student::count = 0;//static在默认的情况下就是 0 啊!?
等价于:
float Student::sum ;
int Student::count ;

关键不在于有没有=0

waizqfor 2009-02-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 cpp_yangzuoshi 的帖子:]
#include <iostream>
using namespace std;
class Student
{public:
Student(int,int,int);
void total();
static float average();
private:
int num;
int age;
float score;
static float sum;
static int count;
};

Student::Student(int m,int a,int s)
{num=m;
age=a;
score=s;
}

float Student::sum = 0;//为何将这两行删掉后编译能通过,但不能执行?

[/Quote]
LZ你拿的什么编译器 那两行我代码 我带着也能通过 运行
去掉了 那你静态的成员变量就没有初始化 也就没办法操作 所以你运行不了啊
tangshuiling 2009-02-16
  • 打赏
  • 举报
回复

荣哥火气挺冲的~~~O(∩_∩)O哈哈~
静态类成员必须单独初始化
xuruichen 2009-02-16
  • 打赏
  • 举报
回复
float Student::sum = 0;//为何将这两行删掉后编译能通过,但不能执行?
int Student::count = 0;//static在默认的情况下就是 0 啊!?
是不是应该直接放在};后面啊。

既然是静态参量,就应该给赋初值。
sagegz 2009-02-16
  • 打赏
  • 举报
回复
VS2005下编译通过,没问题.
arong1234 2009-02-16
  • 打赏
  • 举报
回复
什么叫不能执行?不能执行怎么会把值设上?
谁告诉你默认是0的?系统不会给你“默认”值

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧