值得研究的问题..

「已注销」 2008-11-20 05:58:28

#include <iostream>
using namespace std;
class Student
{
public:
Student()
{
No=0;
Name=" ";
Sum=0.0;
Averages=0.0;
}
~Student(){cout<<"结束统计A类."<<endl;}
void Get(int,char *,float);
void Set();
void Average();
private:
int No;
char *Name;
float Sum;
float Averages;
static int Noo;
};
int Student::Noo = 0;
void Student::Get(int x,char *y,float z)
{
No=x;
Name=y;
Sum=z;
Averages+=Sum;
Noo++;
}
void Student::Set()
{
cout<<"学码:"<<No<<"姓名:"<<Name<<"成绩:"<<Sum<<"分 ";
}

void Student::Average()
{
cout<<"总共有"<<Noo<<"个人的平均分数是:"<<Averages/Noo<<"分.";
}
//////////////////////////////////////////////////////////////////////
class Student2:public Student
{
public:
Student2():Student()
{
Height=Weight=0;
AverageHeight=AverageWeight=0.0;
}
~Student2(){cout<<"结束统计B类."<<endl;}
void Get(int,char *,float,float,float);
void Set();
void Average();
private:
float Height;
float Weight;
float AverageHeight;
float AverageWeight;
};
void Student2::Get(int x,char *y,float z,float h,float w)
{
Student::Get(x,y,z);
Height=h;
Weight=w;
AverageHeight+=Height;
AverageWeight+=Weight;
}
void Student2::Set()
{
Student::Set();
cout<<"身高:"<<Height<<"米"<<"体重:"<<Weight<<"斤"<<endl;
}
void Student2::Average()
{
Student::Average();
cout<<"平均身高:"<<AverageHeight/Student::Noo<<"平均体重:"<<AverageWeight/Student::Noo<<endl;
}

int main()
{
Student2 c;
c.Get(1,"张三",90,1.75,110);
c.Set();
c.Get(2,"李四",85,1.70,100);
c.Set();
c.Get(3,"王五",80,1.65,95);
c.Set();
c.Average();
return 0;
}

这题目主要出在私有的静态度私有的数据成员static int Noo;这
为什么在VC6.0编译执行通过无错
而在VS2005编译就出现无法访问能?
是不是编译器的区别吖?`
...全文
82 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzfxyer 2008-11-20
  • 打赏
  • 举报
回复
虽然说static变量不从属于任何对象,但是作为静态变量,
也不是Student::Noo能访问的。
而且vc6.0对c++的支持那是不用说的。。能有70%就不错了。
longlinghu 2008-11-20
  • 打赏
  • 举报
回复
public:
static int Noo;

用protected的话 貌似只有在它的继承类里调用。
霍大脚 2008-11-20
  • 打赏
  • 举报
回复

把class Student改下,把静态成员改成保护或者公有
class Student
{
public:
Student()
{
No=0;
Name=" ";
Sum=0.0;
Averages=0.0;
}
~Student(){cout<<"结束统计A类."<<endl;}
void Get(int,char *,float);
void Set();
void Average();
private:
int No;
char *Name;
float Sum;
float Averages;
protected:
static int Noo;
};
spotatoes 2008-11-20
  • 打赏
  • 举报
回复
public
霍大脚 2008-11-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 aurora001 的回复:]
protected:
static int Noo;
[/Quote]
是啊,私有的能直接加作用域解析访问吗?
Qlaiaqu 2008-11-20
  • 打赏
  • 举报
回复
Student::Noo被你设置为私有静态成员变量,所以无法访问。你可以设置为public或protected。
hznat 2008-11-20
  • 打赏
  • 举报
回复
友情支持!
aurora001 2008-11-20
  • 打赏
  • 举报
回复
protected:
static int Noo;

65,211

社区成员

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

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