| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1941 人关注过本帖
标题:求助:C++对象数组和指针问题
取消只看楼主 加入收藏
linw1225
Rank: 3Rank: 3
来 自:福建
等 级:论坛游侠
帖 子:110
专家分:145
注 册:2011-4-7
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
求助:C++对象数组和指针问题
程序目的:
建立一个对象数组,内放6个学生数据(学号、成绩),设立一个函数max,使用对象指针作为函数参数,在max函数中找出6个学生中成绩最高者,并输出学号。
代码:
#include<iostream>
using namespace std;

class Student
{
    private:
        int num;
        double score;
    public:
        Student(int n,double s)
        {
            num=n;
            score=s;
        }
        ~Student()
        { }
        
        friend void max(Student *s,Student *s2);
};

[u]void max(Student *s,Student *s2)
{
    double m;
    m=s->score;
    s2=s;
    for(int i=1;i<6;i++,s++)
    {
        if( m<(s->score) )
        {
            m=(s->score);
            s2=s;
        }
    }

   
    cout<<s2->num<<endl;
}

int main()
{
    Student stu[6]={   Student(1,10),Student(2,20),Student(3,30),
                       Student(4,40),Student(5,50),Student(6,60)};
    Student *a,*b;  
    max(a,b);

    return 0;
}
这个程序编译通过了,但是运行会出现错误,应该是划线部分的错误吧?
希望哪位能给出正确代码最好带有注释。
如果能解释下原代码的错误就更好了。
搜索更多相关主题的帖子: private 
2011-05-02 19:31
linw1225
Rank: 3Rank: 3
来 自:福建
等 级:论坛游侠
帖 子:110
专家分:145
注 册:2011-4-7
收藏
得分:0 
回复 楼主 linw1225
能麻烦你给出具体的代码么?
因为说的我有时候不太懂,但是我看代码理解的快些。
麻烦了。

Einmal ist keinmal
2011-05-02 19:56
linw1225
Rank: 3Rank: 3
来 自:福建
等 级:论坛游侠
帖 子:110
专家分:145
注 册:2011-4-7
收藏
得分:0 
回复 3楼 linw1225
要输出的是成绩最大的学生的学号不需要定义两个指针么?

Einmal ist keinmal
2011-05-03 17:48
linw1225
Rank: 3Rank: 3
来 自:福建
等 级:论坛游侠
帖 子:110
专家分:145
注 册:2011-4-7
收藏
得分:0 
回复 5楼 linw1225
麻烦了,很感谢你,看懂了。
然后那个max函数要输出成绩最大者的学号,没必要按你所说的“再定义一个指针s1,将传递进来的指针也复制给s1,再用一个for循环,将每个对象数组的score成员与最大分数进行比较”。这样太麻烦,可以直接定义一个数,代码如下:
void max(Student *s)
{
    double m=0;
    int n;      //表示成绩最大者的学号
    Student *s1=s;

    for(int i=0;i<6;i++,s++)
    {
        if( m<(s->score) )
        {
            m=(s->score);
            n=(s->num);

        }
    }   
   
    cout<<"the max score of the students is: "<<m<<endl;
    cout<<"the max score of the student's study-num is: "<<n<<endl;         
}

能否再问你个问题,你所编的max()函数里面 定义的double m是没有初始化的
for(int i=0;i<6;i++,s++)
    {
        if( m<(s->score) )
        {
            m=(s->score);
        }
    }   
    cout<<"the max score of the students is: "<<m<<endl;
那么这个for循环里面有比较m和s->score的值,为什么可以成功的运行?
就是当i=0的时候,m的值没有定义是通过什么比较的?

Einmal ist keinmal
2011-05-03 22:38
linw1225
Rank: 3Rank: 3
来 自:福建
等 级:论坛游侠
帖 子:110
专家分:145
注 册:2011-4-7
收藏
得分:0 
回复 6楼 Pirelo
麻烦了,很感谢你,看懂了。
然后那个max函数要输出成绩最大者的学号,没必要按你所说的“再定义一个指针s1,将传递进来的指针也复制给s1,再用一个for循环,将每个对象数组的score成员与最大分数进行比较”。这样太麻烦,可以直接定义一个数,代码如下:
void max(Student *s)
{
    double m=0;
    int n;      //表示成绩最大者的学号
    Student *s1=s;

    for(int i=0;i<6;i++,s++)
    {
        if( m<(s->score) )
        {
            m=(s->score);
            n=(s->num);

        }
    }   
   
    cout<<"the max score of the students is: "<<m<<endl;
    cout<<"the max score of the student's study-num is: "<<n<<endl;         
}

能否再问你个问题,你所编的max()函数里面 定义的double m是没有初始化的
for(int i=0;i<6;i++,s++)
    {
        if( m<(s->score) )
        {
            m=(s->score);
        }
    }   
    cout<<"the max score of the students is: "<<m<<endl;
那么这个for循环里面有比较m和s->score的值,为什么可以成功的运行?
就是当i=0的时候,m的值没有定义是通过什么比较的?

Einmal ist keinmal
2011-05-03 22:39
linw1225
Rank: 3Rank: 3
来 自:福建
等 级:论坛游侠
帖 子:110
专家分:145
注 册:2011-4-7
收藏
得分:0 
回复 9楼 Pirelo
好吧,成功解决,麻烦你了,又会了一样。

Einmal ist keinmal
2011-05-04 18:16
快速回复:求助:C++对象数组和指针问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016536 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved