#include <iostream>
using namespace std;
class Teacher
{
private:
int num;
char name[20];
char sex;
float pay;
public:
Teacher();
Teacher(Student &); //语法错误 标识符"Student" error C2535: “Teacher::Teacher(void)”: 已经定义或声明成员函数
Teacher(int n, char nam[],char s,float p);
void display(){cout<<"num:"<<num<<endl<<"name:"<<name<<endl<<"sex:"<<sex<<endl<<"pay:"<<pay<<endl;}
};
class Student
{
private:
int num;
char name[20];
char sex;
float score;
public:
Student(int ,char[],char,float);
friend Teacher;
void display()
{cout<<"num:"<<num<<endl<<"name:"<<name<<endl<<"sex:"<<sex<<endl<<"score:"<<score<<endl;}
};
Teacher::Teacher(Student &s) //未声明标识符's' 未声明标识符‘Student’
{ // error C2448: “Teacher::{ctor}”: 函数样式初始值设定项类似函数定义
num=s.num;
strcpy(name,s.name);
sex=s.sex;
}
Teacher::Teacher(int n, char nam[], char s,float p)
{
num=n;
strcpy(name,nam);
sex=s;
pay=p;
}
Student::Student(int n,char nam[],char s,float sco)
{
num=n;
strcpy(name,nam);
sex=s;
score=sco;
}
int main()
{
Student s1(100101,"zhang",'m',20.5);
Teacher t1;
t1=Teacher(s1); // error C2440: “<function-style-cast>”: 无法从“Student”转换为“Teacher”
t1.display();
return 0;
}
求大神帮忙改代码C++!!!在线等!!!把Student类中的num name sex移植到Teacher类中
答案:4 mip版
解决时间 2021-02-26 04:37
- 提问者网友:童話被染上了傷
- 2021-02-25 09:33
最佳答案
- 二级知识专家网友:花落浅殇
- 2021-02-25 10:32
1、使用Student类需要前向声明,即在Teacher类定义前加class Student;的声明;
2、Teacher(Student&)这不属于复制构造函数,不是编译器能自动调用的,要自己定义;
3、所以主函数中Teacher t1;是无定义的,因为Teacher无默认构造函数;
4、可以直接调用Teacher(Student&);Teacher t1(s1);
最后,teaxcher的pay成员将丢失。
建议,最好用类继承方法:先声明Person类,再声明class Teacher:public Person; class Student:public Person...有兴趣可试一试。
补充一点:不建议使用字符串,而应使用string类,在复制、赋值时避免出错。
2、Teacher(Student&)这不属于复制构造函数,不是编译器能自动调用的,要自己定义;
3、所以主函数中Teacher t1;是无定义的,因为Teacher无默认构造函数;
4、可以直接调用Teacher(Student&);Teacher t1(s1);
最后,teaxcher的pay成员将丢失。
建议,最好用类继承方法:先声明Person类,再声明class Teacher:public Person; class Student:public Person...有兴趣可试一试。
补充一点:不建议使用字符串,而应使用string类,在复制、赋值时避免出错。
全部回答
- 1楼网友:往事叫我剪短发
- 2021-02-25 11:39
这个和不简单 自己想
- 2楼网友:寄出个心动
- 2021-02-25 11:24
1.在Teacher类中使用了Student的对象,需要先在Teacher类之前定义Student类
class Student;
class Teacher
{
2.在main函数中使用Teacher的构造,需要直接传入对象,否则就是使用无参数的构造函数
Teacher t1(s1);
代码如下:
#include <iostream>
using namespace std;
class Student;
class Teacher
{
private:
int num;
char name[20];
char sex;
float pay;
public:
Teacher();
Teacher(Student &); //语法错误 标识符"Student" error C2535: “Teacher::Teacher(void)”: 已经定义或声明成员函数
Teacher(int n, char nam[],char s,float p);
void display(){cout<<"num:"<<num<<endl<<"name:"<<name<<endl<<"sex:"<<sex<<endl<<"pay:"<<pay<<endl;}
};
class Student
{
private:
int num;
char name[20];
char sex;
float score;
public:
Student(int ,char[],char,float);
friend Teacher;
void display()
{cout<<"num:"<<num<<endl<<"name:"<<name<<endl<<"sex:"<<sex<<endl<<"score:"<<score<<endl;}
};
Teacher::Teacher(Student &s) //未声明标识符's' 未声明标识符‘Student’
{ // error C2448: “Teacher::{ctor}”: 函数样式初始值设定项类似函数定义
num=s.num;
strcpy(name,s.name);
sex=s.sex;
}
Teacher::Teacher(int n, char nam[], char s,float p)
{
num=n;
strcpy(name,nam);
sex=s;
pay=p;
}
Student::Student(int n,char nam[],char s,float sco)
{
num=n;
strcpy(name,nam);
sex=s;
score=sco;
}
int main()
{
Student s1(100101,"zhang",'m',20.5);
Teacher t1(s1);
//t1=Teacher(s1); // error C2440: “<function-style-cast>”: 无法从“Student”转换为“Teacher”
t1.display();
return 0;
}
问题解决,请采纳!
- 3楼网友:清酒孤欢
- 2021-02-25 10:55
1.在teacher类中使用了student的对象,需要先在teacher类之前定义student类 class student; class teacher { 2.在main函数中使用teacher的构造,需要直接传入对象,否则就是使用无参数的构造函数 teacher t1(s1); 代码如下: #include <iostream> using namespace std; class student; class teacher { private: int num; char name[20]; char sex; float pay; public: teacher(); teacher(student &); //语法错误 标识符"student" error c2535: “teacher::teacher(void)”: 已经定义或声明成员函数 teacher(int n, char nam[],char s,float p); void display(){cout<<"num:"<<num<<endl<<"name:"<<name<<endl<<"sex:"<<sex<<endl<<"pay:"<<pay<<endl;} }; class student { private: int num; char name[20]; char sex; float score; public: student(int ,char[],char,float); friend teacher; void display() {cout<<"num:"<<num<<endl<<"name:"<<name<<endl<<"sex:"<<sex<<endl<<"score:"<<score<<endl;} }; teacher::teacher(student &s) //未声明标识符's' 未声明标识符‘student’ { // error c2448: “teacher::{ctor}”: 函数样式初始值设定项类似函数定义 num=s.num; strcpy(name,s.name); sex=s.sex; } teacher::teacher(int n, char nam[], char s,float p) { num=n; strcpy(name,nam); sex=s; pay=p; } student::student(int n,char nam[],char s,float sco) { num=n; strcpy(name,nam); sex=s; score=sco; } int main() { student s1(100101,"zhang",'m',20.5); teacher t1(s1); //t1=teacher(s1); // error c2440: “<function-style-cast>”: 无法从“student”转换为“teacher” t1.display(); return 0; } 问题解决,请采纳!
我要举报
如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
点此我要举报以上问答信息
推荐资讯