| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2649 人关注过本帖
标题:老师布置的作业,可是在输入数据后系统崩了?求解。谢谢
只看楼主 加入收藏
过五矿
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2015-10-29
结帖率:57.14%
收藏
 问题点数:0 回复次数:7 
老师布置的作业,可是在输入数据后系统崩了?求解。谢谢
#include <iostream>
#include <string.h>

using namespace std;
class Teacher;
class Student{
public:
    friend class Teacher;
    Student(char *,int);
    ~Student(){}
private:
    char * m_pName;
    int m_iScore;

};

Student::Student(char *name,int score)
{

    m_pName=new char[strlen(name)+1];
    strcpy(m_pName,name);
    m_iScore=score;
}

class Teacher{
public:
    Teacher(int MAXSIZE)
    {
     m_pStu=new Student*[MAXSIZE];
   }
    ~Teacher()
    {
        delete []m_pStu ;
        m_pStu=NULL;
    }
    void Insert(int);
    void Sort(int);
    void InfoPrint(int);
private:
    Student **m_pStu;
};


void Teacher::Insert(int MAXSIZE)
{
    char *name;
    float score;
    for(int i=0;i<MAXSIZE;i++)
    {
    cout<<"please input the information of the new student";
    cin>>name;
    cin>>score;
    Student *newStu=new Student(name,score);
    m_pStu=&newStu;
    m_pStu=m_pStu+1;
    }

}

void Teacher::Sort(int MAXSIZE)
{
    Student **temp1,**temp2,**temp;
   for(int i=0 ; i<MAXSIZE-1 ;i++)
    for(int j=0; j<MAXSIZE-1-i; j++)
   {
       if( (*(m_pStu+i))->m_iScore > (*(m_pStu+i+1))->m_iScore )
       {
           temp1=m_pStu+i;
           temp2=m_pStu+i+1;
           temp=temp1;
           temp1=temp2;
           temp2=temp;

       }
   }
}
void Teacher::InfoPrint(int MAXSIZE)
{
    for(int i=0;i<MAXSIZE;i++)
    {

        cout<<i<<" Name:"<<(m_pStu[i]->m_pName)<<endl;
        cout<<i<<" Score:"+(*(m_pStu+i))->m_iScore<<endl;
        cout<<endl;
    }
}
int main()
{
    int MAXSIZE;
    cout << "Please input the student's number" << endl;
    cin>>MAXSIZE;

    Teacher teacher(MAXSIZE);
    teacher.Insert(MAXSIZE);
    teacher.Sort(MAXSIZE);
    teacher.InfoPrint(MAXSIZE);
    return 0;
}
[code][/
图片附件: 游客没有浏览图片的权限,请 登录注册
code]
搜索更多相关主题的帖子: private include public friend 
2016-03-15 22:32
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:0 
[09:58:25 azzbcc@XJH-PC src]$ g++ -W -Wall -O2 CPP.cpp

CPP.cpp: In member function ‘void Teacher::Insert(int)’:
CPP.cpp:54:20: warning: ‘name’ may be used uninitialized in this function [-Wmaybe-uninitialized]
         cin >> name;
                    ^


[fly]存在即是合理[/fly]
2016-03-16 09:58
仰望星空的
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:50
专家分:248
注 册:2015-9-28
收藏
得分:0 
    Student *newStu=new Student(name,score);
这句开辟指针,然后没有释放的原因?
2016-03-16 14:56
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
void Teacher::Insert(int MAXSIZE)
{
    char *name;
    float score;
    for(int i=0;i<MAXSIZE;i++)
    {
    cout<<"please input the information of the new student";
    cin>>name;
    cin>>score;
    Student *newStu=new Student(name,score);
    m_pStu=&newStu;
    m_pStu=m_pStu+1;
    }

}
这个for循环的最后两句是想做什么?感觉有点莫明其妙。
2016-03-16 15:53
过五矿
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2015-10-29
收藏
得分:0 
回复 4楼 yangfrancis
就是把每一个新开辟的对象储存到teacher中得**pstu中。
2016-03-16 18:55
过五矿
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2015-10-29
收藏
得分:0 
回复 3楼 仰望星空的
我试了释放掉内存,程序还是崩了
2016-03-16 19:02
过五矿
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2015-10-29
收藏
得分:0 
回复 4楼 yangfrancis
老师不让用继承,只能用友元类,所以设置了一个双重指针来储存学生的地址,实现管理
2016-03-16 19:03
过五矿
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2015-10-29
收藏
得分:0 
我知道为什么崩了,因为name没有分配空间,所以局部变量用完就释放掉了。谢谢大家
2016-03-16 19:40
快速回复:老师布置的作业,可是在输入数据后系统崩了?求解。谢谢
数据加载中...
 
   



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

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