| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1941 人关注过本帖
标题:求助:C++对象数组和指针问题
只看楼主 加入收藏
linw1225
Rank: 3Rank: 3
来 自:福建
等 级:论坛游侠
帖 子:110
专家分:145
注 册:2011-4-7
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:9 
求助: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
Pirelo
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:118
专家分:550
注 册:2011-1-28
收藏
得分:0 
Student *a,*b;
定义的对象指针没被赋初值就直接使用了,建议楼主max()函数的形参还是用对象的引用吧
收到的鲜花
  • linw12252011-05-03 22:58 送鲜花  5朵   附言:我很赞同
2011-05-02 19:52
linw1225
Rank: 3Rank: 3
来 自:福建
等 级:论坛游侠
帖 子:110
专家分:145
注 册:2011-4-7
收藏
得分:0 
回复 楼主 linw1225
能麻烦你给出具体的代码么?
因为说的我有时候不太懂,但是我看代码理解的快些。
麻烦了。

Einmal ist keinmal
2011-05-02 19:56
Pirelo
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:118
专家分:550
注 册:2011-1-28
收藏
得分:0 
我试过了,引用确实不好使,非常抱歉先前没有仔细看,不过楼主使用指针一定要初始化,根据你的代码我改了一下,max只传递一个指针进去:

#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);
};

void max(Student *s)
{
    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;
}

int main()
{
    Student stu[6]={   Student(1,300),Student(2,20),Student(3,30),
                       Student(4,40),Student(5,50),Student(6,60)};
    Student *a=stu;  
    max(a);
    system("pause");
    return 0;
}
收到的鲜花
  • linw12252011-05-03 22:58 送鲜花  5朵   附言:我很赞同
  • linw12252011-05-03 22:59 送鲜花  5朵   附言:我很赞同
2011-05-02 22:54
linw1225
Rank: 3Rank: 3
来 自:福建
等 级:论坛游侠
帖 子:110
专家分:145
注 册:2011-4-7
收藏
得分:0 
回复 3楼 linw1225
要输出的是成绩最大的学生的学号不需要定义两个指针么?

Einmal ist keinmal
2011-05-03 17:48
Pirelo
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:118
专家分:550
注 册:2011-1-28
收藏
得分:20 
回复 5楼 linw1225
哦,还要输出学号,这也很简单啊,可以在max()内再定义一个指针s1,将传递进来的指针也复制给s1,再用一个for循环,将每个对象数组的score成员与最大分数进行比较:
if(m==s1->score),若表达式成立,就输出s1->num,即为对应的学号:
#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);
};

void max(Student *s)
{
    double m;
    Student *s1=s;
//find the max srore
    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;
//print the corresponding num   
    for(int j=0;j<6;j++,s1++)
    {
        
        if(m==(s1->score))
        {
            cout<<"the max score of the student's study-num is: "<<s1->num;
        }
    }
}

int main()
{
    Student stu[6]={   Student(1,300),Student(2,20),Student(3,30),
                       Student(4,40),Student(5,50),Student(6,60)};
    Student *a=stu;  
    max(a);
    system("pause");
    return 0;
}



[ 本帖最后由 Pirelo 于 2011-5-3 19:06 编辑 ]
收到的鲜花
  • linw12252011-05-03 22:59 送鲜花  5朵   附言:我很赞同
2011-05-03 18:27
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
Pirelo
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:118
专家分:550
注 册:2011-1-28
收藏
得分:0 
回复 8楼 linw1225
楼主很细心,我很粗心,
m未赋初值并非我的本意。
对于未赋初值的全局变量和静态变量,某些系统会默认把它们的值置成0,可惜m在这里两者都不是,
因此这个程序因m的未赋初值而存在bug,我测试了当i=0时,m=-9.25596e+061,是一个很小的负数,因此拿这样一个负数去和全部为正数的score比较,在for循环里能够执行成功,实属侥幸。
2011-05-04 09:40
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.052381 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved