C++编程题,不知道怎么改
#include<iostream.h>classteacher{public:intteacher_num;char*teacher_name;teacher(intn...
#include<iostream.h>
class teacher
{
public:
int teacher_num;
char* teacher_name;
teacher(int n,char* m);
void show()
{
cout<<"班主任:"<<endl;
cout<<"教工号:"<<teacher_num<<endl;
cout<<"姓名:"<<teacher_name<<endl;
}
};
teacher::teacher(int n,char* m)
{
teacher_num=n;
teacher_name=m;
}
class student
{
public:
int student_num;
char* student_name;
teacher t;
student(int a,char *b);
void show()
{
cout<<"学生:"<<endl;
cout<<"学号:"<<student_num<<endl;
cout<<"姓名:"<<student_name<<endl;
t.show();
}
};
student::student(int a,char *b)
{ (显示错误的行)
student_num=a;
student_name=b;
}
void main()
{
char* one="Lee";
char* two="Alice";
teacher t(000001,one);
student stu(28,two);
stu.show();
}
出现错误:no appropriate default constructor available
请各位大虾帮我改一下,感激不尽 展开
class teacher
{
public:
int teacher_num;
char* teacher_name;
teacher(int n,char* m);
void show()
{
cout<<"班主任:"<<endl;
cout<<"教工号:"<<teacher_num<<endl;
cout<<"姓名:"<<teacher_name<<endl;
}
};
teacher::teacher(int n,char* m)
{
teacher_num=n;
teacher_name=m;
}
class student
{
public:
int student_num;
char* student_name;
teacher t;
student(int a,char *b);
void show()
{
cout<<"学生:"<<endl;
cout<<"学号:"<<student_num<<endl;
cout<<"姓名:"<<student_name<<endl;
t.show();
}
};
student::student(int a,char *b)
{ (显示错误的行)
student_num=a;
student_name=b;
}
void main()
{
char* one="Lee";
char* two="Alice";
teacher t(000001,one);
student stu(28,two);
stu.show();
}
出现错误:no appropriate default constructor available
请各位大虾帮我改一下,感激不尽 展开
3个回答
展开全部
你定义teacher类的时候没有找到默认值的构造函数。你如果要这样定义teacher t的话,就得把teacher类的构造函数做一个默认值,比如:teacher::teacher(int n=0,char* m="wang")
{
teacher_num=n;
teacher_name=m;
}
或者你在定义teacher t的时候给定一个值。比如:
teacher t(1001,"wang");
这样就不会出现没有默认构造函数的情况了。
{
teacher_num=n;
teacher_name=m;
}
或者你在定义teacher t的时候给定一个值。比如:
teacher t(1001,"wang");
这样就不会出现没有默认构造函数的情况了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Strdent类里有个Teacher类的对象,但是没有按照Teacher类的构造函数进行实例化。
看你的程序是要显示一个学生和他对应的老师,可以把Student类的构造函数改成这样:
Student::Student(int sNum, char* sName, int tNum, char* tName):Teacher(tNum, tName)
{
student_num = sNum;
strcpy(student_name, sName);
}
main函数调用的时候只要实例化Student类,就行了。
看你的程序是要显示一个学生和他对应的老师,可以把Student类的构造函数改成这样:
Student::Student(int sNum, char* sName, int tNum, char* tName):Teacher(tNum, tName)
{
student_num = sNum;
strcpy(student_name, sName);
}
main函数调用的时候只要实例化Student类,就行了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream.h>
#include <string.h>
class teacher
{
public:
int teacher_num;
char* teacher_name;
teacher(int n,char* m);
teacher()
{
}
~teacher()
{}
void show()
{
cout<<"班主任:"<<endl;
cout<<"教工号:"<<teacher_num<<endl;
cout<<"姓名:"<<teacher_name<<endl;
}
};
teacher::teacher(int n,char* m)
{
teacher_num=n;
teacher_name=m;
}
class student
{
public:
int student_num;
char* student_name;
teacher t;
student(int a,char *b);
student();
~student();
void show()
{
cout<<"学生:"<<endl;
cout<<"学号:"<<student_num<<endl;
cout<<"姓名:"<<student_name<<endl;
t.show();
}
};
student::student()
{
}
student::~student()
{
}
student::student(int a,char *b)
{
student_num=a;
// student_name=b;//
strcpy(student_name,b);
}
void main()
{
char* one="Lee";
char* two="Alice";
teacher t(000001,one);
student stu(28,two);
stu.show();
}
把默认的构造函数和析构函数加上就行了
#include <string.h>
class teacher
{
public:
int teacher_num;
char* teacher_name;
teacher(int n,char* m);
teacher()
{
}
~teacher()
{}
void show()
{
cout<<"班主任:"<<endl;
cout<<"教工号:"<<teacher_num<<endl;
cout<<"姓名:"<<teacher_name<<endl;
}
};
teacher::teacher(int n,char* m)
{
teacher_num=n;
teacher_name=m;
}
class student
{
public:
int student_num;
char* student_name;
teacher t;
student(int a,char *b);
student();
~student();
void show()
{
cout<<"学生:"<<endl;
cout<<"学号:"<<student_num<<endl;
cout<<"姓名:"<<student_name<<endl;
t.show();
}
};
student::student()
{
}
student::~student()
{
}
student::student(int a,char *b)
{
student_num=a;
// student_name=b;//
strcpy(student_name,b);
}
void main()
{
char* one="Lee";
char* two="Alice";
teacher t(000001,one);
student stu(28,two);
stu.show();
}
把默认的构造函数和析构函数加上就行了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询