求帮忙看一下我这到C++程序哪里错了,谢谢诶
#include<iostream.h>#include<string.h>classstudent{private:charname[20];longsid;float...
#include <iostream.h>
#include <string.h>
class student
{
private:
char name[20];
long sid;
float score;
public:
void set()
{
cout<<"please input the student's sid:";cin>>sid;
cout<<"please input the student's score:";cin>>score;
cout<<"please input the student's name:";cin>>name;
}
friend void order(int n);
};
void ord(int n)
{
int i;
student stu[20];
for(i=0;i<n;i++)
{
float maxs=stu[0].score;
char maxn[20];
long maxid;float ave;
int i;
ave+=stu[i].score;
if(max<stu[i].score)
{
max=stu[i].score;
strcpy(maxn,stu[i].name);
maxid=stu[i].sid;
}
}
ave/=n;
}
void main()
{
student stu[20];
int i,n;
cout<<"please input n=:";cin>>n;
for(i=0;i<n;i++)
{
cout<<"the"<<i<<"student's info:"<<endl;
stu[i].set();
}
order(n);
cout<<"the highest score goes to:"<<endl;
cout<<maxid<<" "<<name<<" "<<score<<endl;
cout<<"the average score is:"<<ave;
}
:\个人\20091957\p4\4.cpp(29) : error C2248: 'score' : cannot access private member declared in class 'student'
D:\个人\20091957\p4\4.cpp(11) : see declaration of 'score'
D:\个人\20091957\p4\4.cpp(31) : error C2018: unknown character '0xa3'
D:\个人\20091957\p4\4.cpp(31) : error C2018: unknown character '0xbb'
D:\个人\20091957\p4\4.cpp(31) : error C2144: syntax error : missing ';' before type 'float'
D:\个人\20091957\p4\4.cpp(34) : error C2248: 'score' : cannot access private member declared in class 'student'
D:\个人\20091957\p4\4.cpp(11) : see declaration of 'score'
D:\个人\20091957\p4\4.cpp(35) : error C2065: 'max' : undeclared identifier
D:\个人\20091957\p4\4.cpp(35) : error C2248: 'score' : cannot access private member declared in class 'student'
D:\个人\20091957\p4\4.cpp(11) : see declaration of 'score'
D:\个人\20091957\p4\4.cpp(37) : error C2248: 'score' : cannot access private member declared in class 'student'
D:\个人\20091957\p4\4.cpp(11) : see declaration of 'score'
D:\个人\20091957\p4\4.cpp(37) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
D:\个人\20091957\p4\4.cpp(38) : error C2248: 'name' : cannot access private member declared in class 'student'
D:\个人\20091957\p4\4.cpp(9) : see declaration of 'name'
D:\个人\20091957\p4\4.cpp(39) : error C2248: 'sid' : cannot access private member declared in class 'student'
D:\个人\20091957\p4\4.cpp(10) : see declaration of 'sid'
D:\个人\20091957\p4\4.cpp(42) : error C2065: 'ave' : undeclared identifier
D:\个人\20091957\p4\4.cpp(57) : error C2065: 'maxid' : undeclared identifier
D:\个人\20091957\p4\4.cpp(57) : error C2065: 'name' : undeclared identifier
D:\个人\20091957\p4\4.cpp(57) : error C2065: 'score' : undeclared identifier
执行 cl.exe 时出错
我自己找到D:\个人\20091957\p4\4.cpp(42) : error C2065: 'ave' : undeclared identifier
D:\个人\20091957\p4\4.cpp(57) : error C2065: 'maxid' : undeclared identifier
D:\个人\20091957\p4\4.cpp(57) : error C2065: 'name' : undeclared identifier
D:\个人\20091957\p4\4.cpp(57) : error C2065: 'score' : undeclared identifier
执行 cl.exe 时出错
题目是:// 输入若干个学生的数据(包括学号、姓名和成绩),编程:(要求定义一个student类,用友元函数实现)
//1) 计算所有学生成绩的平均值,并输出。
//2) 找出学生中的成绩最高者,输出该学生的学号、姓名和成绩。 展开
#include <string.h>
class student
{
private:
char name[20];
long sid;
float score;
public:
void set()
{
cout<<"please input the student's sid:";cin>>sid;
cout<<"please input the student's score:";cin>>score;
cout<<"please input the student's name:";cin>>name;
}
friend void order(int n);
};
void ord(int n)
{
int i;
student stu[20];
for(i=0;i<n;i++)
{
float maxs=stu[0].score;
char maxn[20];
long maxid;float ave;
int i;
ave+=stu[i].score;
if(max<stu[i].score)
{
max=stu[i].score;
strcpy(maxn,stu[i].name);
maxid=stu[i].sid;
}
}
ave/=n;
}
void main()
{
student stu[20];
int i,n;
cout<<"please input n=:";cin>>n;
for(i=0;i<n;i++)
{
cout<<"the"<<i<<"student's info:"<<endl;
stu[i].set();
}
order(n);
cout<<"the highest score goes to:"<<endl;
cout<<maxid<<" "<<name<<" "<<score<<endl;
cout<<"the average score is:"<<ave;
}
:\个人\20091957\p4\4.cpp(29) : error C2248: 'score' : cannot access private member declared in class 'student'
D:\个人\20091957\p4\4.cpp(11) : see declaration of 'score'
D:\个人\20091957\p4\4.cpp(31) : error C2018: unknown character '0xa3'
D:\个人\20091957\p4\4.cpp(31) : error C2018: unknown character '0xbb'
D:\个人\20091957\p4\4.cpp(31) : error C2144: syntax error : missing ';' before type 'float'
D:\个人\20091957\p4\4.cpp(34) : error C2248: 'score' : cannot access private member declared in class 'student'
D:\个人\20091957\p4\4.cpp(11) : see declaration of 'score'
D:\个人\20091957\p4\4.cpp(35) : error C2065: 'max' : undeclared identifier
D:\个人\20091957\p4\4.cpp(35) : error C2248: 'score' : cannot access private member declared in class 'student'
D:\个人\20091957\p4\4.cpp(11) : see declaration of 'score'
D:\个人\20091957\p4\4.cpp(37) : error C2248: 'score' : cannot access private member declared in class 'student'
D:\个人\20091957\p4\4.cpp(11) : see declaration of 'score'
D:\个人\20091957\p4\4.cpp(37) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
D:\个人\20091957\p4\4.cpp(38) : error C2248: 'name' : cannot access private member declared in class 'student'
D:\个人\20091957\p4\4.cpp(9) : see declaration of 'name'
D:\个人\20091957\p4\4.cpp(39) : error C2248: 'sid' : cannot access private member declared in class 'student'
D:\个人\20091957\p4\4.cpp(10) : see declaration of 'sid'
D:\个人\20091957\p4\4.cpp(42) : error C2065: 'ave' : undeclared identifier
D:\个人\20091957\p4\4.cpp(57) : error C2065: 'maxid' : undeclared identifier
D:\个人\20091957\p4\4.cpp(57) : error C2065: 'name' : undeclared identifier
D:\个人\20091957\p4\4.cpp(57) : error C2065: 'score' : undeclared identifier
执行 cl.exe 时出错
我自己找到D:\个人\20091957\p4\4.cpp(42) : error C2065: 'ave' : undeclared identifier
D:\个人\20091957\p4\4.cpp(57) : error C2065: 'maxid' : undeclared identifier
D:\个人\20091957\p4\4.cpp(57) : error C2065: 'name' : undeclared identifier
D:\个人\20091957\p4\4.cpp(57) : error C2065: 'score' : undeclared identifier
执行 cl.exe 时出错
题目是:// 输入若干个学生的数据(包括学号、姓名和成绩),编程:(要求定义一个student类,用友元函数实现)
//1) 计算所有学生成绩的平均值,并输出。
//2) 找出学生中的成绩最高者,输出该学生的学号、姓名和成绩。 展开
3个回答
展开全部
#include <iostream.h>
#include <string.h>
class student
{
public://如果你想用私有,可以在public里用函数返回值:类型 get**{return ***;}
char name[20];
long sid;
float score;
void set()
{
cout<<"please input the student's sid:";cin>>sid;
cout<<"please input the student's score:";cin>>score;
cout<<"please input the student's name:";cin>>name;
}
friend int order(int n,float &ave);
};
int order(int n,float &ave,student stu[])
{
int i,max=0;
long maxid;
char maxn[20];
float maxs;
for(i=0;i<n;i++)
{
maxs=stu[i].score;
ave+=stu[i].score;
if(maxs<stu[i].score)
{
maxs=stu[i].score;
strcpy(maxn,stu[i].name);
maxid=stu[i].sid;
max = i;
}
}
ave/=n;
return max;
}
void main()
{
student stu[20];
int i,n,max;
float ave=0;
cout<<"please input n=:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"the"<<i<<"student's info:"<<endl;
stu[i].set();
}
max = order(n,ave,stu);
cout<<"the highest score goes to:"<<endl;
cout<<stu[max].sid<<" "<<stu[max].name<<" "<<stu[max].score<<endl;
cout<<"the average score is:"<<ave;
}
//你可以拿去参考下这个程序,我帮你修改的
#include <string.h>
class student
{
public://如果你想用私有,可以在public里用函数返回值:类型 get**{return ***;}
char name[20];
long sid;
float score;
void set()
{
cout<<"please input the student's sid:";cin>>sid;
cout<<"please input the student's score:";cin>>score;
cout<<"please input the student's name:";cin>>name;
}
friend int order(int n,float &ave);
};
int order(int n,float &ave,student stu[])
{
int i,max=0;
long maxid;
char maxn[20];
float maxs;
for(i=0;i<n;i++)
{
maxs=stu[i].score;
ave+=stu[i].score;
if(maxs<stu[i].score)
{
maxs=stu[i].score;
strcpy(maxn,stu[i].name);
maxid=stu[i].sid;
max = i;
}
}
ave/=n;
return max;
}
void main()
{
student stu[20];
int i,n,max;
float ave=0;
cout<<"please input n=:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"the"<<i<<"student's info:"<<endl;
stu[i].set();
}
max = order(n,ave,stu);
cout<<"the highest score goes to:"<<endl;
cout<<stu[max].sid<<" "<<stu[max].name<<" "<<stu[max].score<<endl;
cout<<"the average score is:"<<ave;
}
//你可以拿去参考下这个程序,我帮你修改的
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
29行的分号你写成中文输入法写的了。
另外,你那些都定义的私有成员啊。知道私有成员干嘛的吗?就是不能从类外访问。就不能用stu.score,stu.name这种形式。你可以设置成public的。方法二:可以提供函数去获得score。比如你在类里面定义一个 float getScore(){return score;}然后,这个是public。你用 stu.getScore()可以获得.
另外,你那些都定义的私有成员啊。知道私有成员干嘛的吗?就是不能从类外访问。就不能用stu.score,stu.name这种形式。你可以设置成public的。方法二:可以提供函数去获得score。比如你在类里面定义一个 float getScore(){return score;}然后,这个是public。你用 stu.getScore()可以获得.
追问
恩,谢谢。这个程序我一直该,自己改的面目全非,我重新编一下,再来提问
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
错太多了!没办法给你改 要实现什么功能啊!类中的私有成员不能直接访问啊 要用公又成员去访问才行! 而公有成员要用类定义的变量访问啊 而且 你程序中还有中文的分号 汗!公有成员函数你怎么写到 主函数里面去了!要在类里面直接定义功能啊!
追问
不好意思,主要是错之后一直改,面目全非了。
我在调重新编一下,再来提问。
多谢
程序有一行是这样的: ave+=stu[i].score,stu[i]是我定义的对象数组,然后编译时显示这一行的错误是:left of '.score' must have class/struct/union type,大致应该是说左边的stu[i]有问题,但我不知是什么问题。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询