``````//student.h
class student
{
public:
student(int number=0,int scores=0);
void input();
void show();
int max(student stu[],int i);
private:
int number;
int scores;
};

``````
``````//student.cpp
#include"student.h"
#include<iostream>
using namespace std;
student::student(int number,int scores)
{
this->number=number;
this->scores=scores;
}

void student::input()
{
cin>>number>>scores;
}

void student::show()
{
cout<<"the hightest scores is"<<&student::max<<endl;
}

int student::max(student stu[],int i)
{   int max;
student *p;
for(i=0;i<5;i++)
{
p[i].scores<p[i+1].scores;
max=p[i+1].scores;
}
return max;
}
``````
``````//main.cpp
#include<iostream>
#include"student.h"
using namespace std;
int main()
{
student stu[5];
student *st;
int i;
for(i=0;i<5;i++){
cout<<"请输入第"<<i+1<<"号学生的信息"<<endl;
stu[i].input();
}
st->max(stu,5);
system ("pause");
return 0;
}

``````

2个回答

1.这里max是函数，而不是成员变量。

``````void student::show()
{
cout<<"the hightest scores is"<<&student::max<<endl;
}
``````

2.这里，函数名max和局部变量max重名了，要改掉一个。
3.max计算的不是最大值，与题意不符。
4.p没有初始化，要加上p = &stu[0]

``````int student::max(student stu[],int i)
{   int max;
student *p;
for(i=0;i<5;i++)
{
p[i].scores<p[i+1].scores;
max=p[i+1].scores;
}
return max;
}
``````

``````int student::max(student stu[],int n)
{   int maxs = 0;
int maxn = 0;
int i;
student *p = &stu[0];
for(i=0;i<n;i++)
{
if (p[i].scores > max) {
maxs = p[i].scores;
maxn = p[i].number;
}
}
return maxn;
}
``````

5.最后，输出漏了。

``````int student::max(student stu[],int i)
{   int ma = 0;
for(i=0;i<i;i++)
{
if (stu[i].scores>stu[ma].scores)
ma=i;
}
return ma;
}

st->max(stu,5);
->
st = &stu[0];
int ma = st->max(stu, 5);
cout << "max student is " << stu[ma].number << endl;
``````