C++中指针参数在虚拟继承中出现错误

月光散落 发布于 2016/04/30 15:25
阅读 59
收藏 0

代码为:

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


//write your code here
class Person {
    private:
        char *name, *tel;
    public:
        Person(char *n, char *t) {
        strcpy(name, n);
        strcpy(tel, t);
}
        char *GetName() {
            return name;
}
        char *GetTel() {
            return tel;
}
};
class Student : virtual public Person {
    private:
        char *major;
    public:
        Student(char *n, char *t, char *m) :Person(n, t) {
            strcpy(major, m);
}
        char *GetMajor() {
            return major;
}
};
class Teacher : virtual public Person {
    private:
        char *course;
    public:
        Teacher(char *n, char *t, char *c) :Person(n, t) {
            strcpy(course, c);
}
        char *GetCourse() {
            return course;
}
};
class StudentTeacher : public Student, public Teacher {
    public:
        StudentTeacher(char *n, char *t, char *m, char *c) :Person(n, t), Student(n, t, m), Teacher(n, t, c) {}

};
int main()
{
char name[40], tel[15], major[18], course[15];// 分别代表名字、电话、专业、课程
cin >> name >> tel >> major >> course;
StudentTeacher st(name, tel, major, course);
cout << st.GetName() << endl;
cout << st.GetTel() << endl;
cout << st.GetMajor() << endl;
cout << st.GetCourse() << endl;
return 0;
}

错误信息为:

加载中
返回顶部
顶部