# include<iostream.h>
# include<string.h>
class Student
{
private:
char *name;
char *direction;
int sno;
public:
~Student(){delete []name;delete []direction;}
Student(char *sn,char *dt,int no);
char * get_s_n(){return name;}
char * get_s_d(){return direction;}
int get_s_sno(){return sno;}
};
Student::Student(char *sn,char *dt,int no)
{
name=new char[strlen(sn)+1];
strcpy(name,sn);
direction=new char[strlen(dt)+1];
strcpy(direction,dt);
sno=no;
}
class Teacher
{
private:
char *name;
Student stu[3];
public:
Teacher(char *tn,Student s[],int n);
~Teacher(){delete []name;}
char * get_t_n(){return name;}
};
Teacher::Teacher(char *tn,Student s[],int n)//错误在这里no appropriate default constructor available
Error executing cl.exe.
{
name=new char[strlen(tn)+1];
strcpy(name,tn);
for(int i=0;i<n;i++)
{ stu[i]=s[i];
}
}
int main()
{
Student stu1("AA","aa",101);
Student stu2("BB","bb",102);
Student stu3("CC","cc",103);
Student stu[]={stu1,stu2,stu3};
//stu[0]=stu1;stu[1]=stu2;stu[2]=stu3;
Teacher t1("XX",stu,3);
cout<<"TEACHER:"<<t1.get_t_n()<<endl;
for(int i=0;i<3;i++)
{ cout<<"STUDENTS:"<<stu[i].get_s_n()<<" "<<stu[i].get_s_d()<<" "<<stu[i].get_s_sno()<<endl; }
return 0;
}
=============
构造函数有问题,帮改下
4 个解决方案
#1
用string取代char *
#2
Student类中加一个 默认构造函数Student(){}
#3
Student类中加一个 默认构造函数Student(){}
=====
why
=====
why
#4
i see
#1
用string取代char *
#2
Student类中加一个 默认构造函数Student(){}
#3
Student类中加一个 默认构造函数Student(){}
=====
why
=====
why
#4
i see