Seemyeyes7 2019-12-24 15:40 采纳率: 50%
浏览 171
已采纳

麻烦大佬帮忙看一下我的代码哪里出了问题,关于对象数组和指针的!!

建立一个对象数组,内放五个学生的数据(学号、成绩)。设立一个函数max,使用对象指针作为函数参数,在max函数中找出5个学生成绩最高者,并输出其学号。要求采用多文件结构实现。

//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条回答 默认 最新

  • bobhuang 2019-12-24 16:01
    关注

    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.最后,输出漏了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题