标题:C++输出了乱码,求解?
取消只看楼主
过五矿
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2015-10-29
结帖率:57.14%
已结贴  问题点数:10 回复次数:2 
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
过五矿
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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-462579-1-1.html




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

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