| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2710 人关注过本帖
标题:C++输出了乱码,求解?
只看楼主 加入收藏
过五矿
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2015-10-29
结帖率:57.14%
收藏
已结贴  问题点数:10 回复次数:3 
C++输出了乱码,求解?
#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";
     name=new char[10];
     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;
 }
[local]1[/local]


无标题.png (24.81 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: private include public friend 
2016-03-16 19:39
wengbin
Rank: 10Rank: 10Rank: 10
来 自:陕西西安
等 级:贵宾
威 望:19
帖 子:370
专家分:1846
注 册:2015-5-8
收藏
得分:10 
使用时一定要关注类对象的生命周期,出现乱码很可能是在使用对象之前对象已经被析构了,你可以在构造类时为析构函数加入一个输出,标识它的生命周期来确认一下
2016-03-16 20:00
过五矿
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2015-10-29
收藏
得分:0 
回复 2楼 wengbin
好像不是因为析构函数,因为没有输出。证明析构函数还没有被执行。不过还是谢谢你热心的解答。
2016-03-16 22:19
过五矿
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2015-10-29
收藏
得分:0 
我知道为什么错了。
Student *newStu=new Student(name,score);
      m_pStu=&newStu;
      m_pStu=m_pStu+1;
问题是这个。改成
  cout<<"please input the information of the new student";
     name=new char[10];
     cin>>name;
     cin>>score;
     m_pStu[i]=  new Student(name,score);
就可以正常运行了
2016-03-17 13:11
快速回复:C++输出了乱码,求解?
数据加载中...
 
   



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

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