回 帖 发 新 帖 刷新版面

主题:错误在哪里啊??

#include<iostream>
#include<string>
using namespace std;
class Person
{ public:
   Person(char*number,char*thename);
   char*getnumber();
   char*getname();
protected:
char*Nu;
char*name;

};
   Person::Person(char*number,char*thename)
   {Nu=number;
    name=thename;
   }
  char *Person::getnumber()
  {return Nu;
  }
   char *Person::getname()
   {return name;
   }
class Student:public Person
{public:
 Student();
 Student(char*number,char*thename,char*researchs):Person(number,thename)
 {research=researchs;}
char*getresearchs()
{return research;}
protected:
char*research;
};
class Teacher:public Person
{ public:
  int ii;
  
 Teacher(char*number,char*thename,int i=0):Person(number,thename){}
    void set()
 {ii=0;}
 void add_a_student(Student&stu)     
 {s[ii]=stu;
        ii++;
 }
void display();
protected:
char*mings;
Student s[20];

  
};
void Teacher::display()
{ cout<<"研究生导师:"<<"\n"<<"\t"<<Nu<<","<<name<<endl;
    cout<<"研究生:"<<endl;
   for(int i=0;i<ii;i++)
   {
  s[i].getnumber();
      s[i].getname();
      s[i].getresearchs();
   }
};
void main()
{Teacher a("t1","黄东军");
 Teacher b("t2","张云峰");
 Student s1("99101","陈磊","人工智能");
 Student s2("99102","崔大红","数据仓库");
 Student s3("99103","王涛涛","数据结构");
 Student s4("99104","宋丹","计算机网络");
 a.add_a_student(s1);
 a.add_a_student(s2);
 a.add_a_student(s3);
 b.add_a_student(s4);
 a.display();
 b.display();
 
}
请哪位高手帮忙改一下咯。。

回复列表 (共2个回复)

沙发

你的构造函数有问题 因为其中有指针赋值问题

板凳


[code=c]
// shishi.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


#include<iostream>
#include<string>
using namespace std;


class Person
{ public:
Person(char*number,char*thename);
/////////////////////////////////////////
Person(){Nu=new char; name=new char;};
//~Person(){delete Nu;delete name;}; 我要打算写下析构函数的  不知道哪出错了 写上就运行出错
//////////////////////////////////////////
char*getnumber();
char*getname();
protected:
    char*Nu;
    char*name;

};
Person::Person(char*number,char*thename)
{Nu=new char; name=new char;
Nu=number;
name=thename;
}
char *Person::getnumber()
{return Nu;
}
char *Person::getname()
{return name;
}

class Student:public Person
{public:
///////////////////////////
Student(){Nu=new char; name=new char;};
////////////////////////////
Student(char*number,char*thename,char*researchs):Person(number,thename)
{   
    research=researchs;}
char*getresearchs()
{return research;}
protected:
    char*research;
};


class Teacher:public Person
{ public:
int ii;

Teacher(char*number,char*thename,int i=0):Person(number,thename){ii=i;}
void set()
{ii=0;}
void add_a_student(Student&stu)     
{s[ii]=stu;
ii++;
}
void display();
protected:
    char*mings;
    Student s[20];


};
void Teacher::display()
{ cout<<"研究生导师:"<<"\n"<<"\t"<<Nu<<","<<name<<endl;
cout<<"研究生:"<<endl;
for(int i=0;i<ii;i++)
{
    s[i].getnumber();
    s[i].getname();
    s[i].getresearchs();
}
};

void main()
{Teacher a("t1","黄东军");
Teacher b("t2","张云峰");
Student s1("99101","陈磊","人工智能");
Student s2("99102","崔大红","数据仓库");
Student s3("99103","王涛涛","数据结构");
Student s4("99104","宋丹","计算机网络");
a.add_a_student(s1);
a.add_a_student(s2);
a.add_a_student(s3);
b.add_a_student(s4);
a.display();
b.display();

}
[/code]

我把你的默认构造函数补上就可以运行了

我来回复

您尚未登录,请登录后再回复。点此登录或注册