类的继承错误 初学 请多指教
程序代码:
//person是student,teacher的基类。而teacher_student又是student和teacher的子类。 //并且继承之间都是pubilc 且无虚继承。 //错误发生在 Teacher_student类的writein_tofile()函数的第三行"out<<name<<" "<<sex<<" "<<age<<" "<<IDstudent<<" "<<IDclass<<" "<<IDteacher<<'\n';" // error C2385: 对“name”的访问不明确 // 1> 可能是“name”(位于基“Person”中) // 1> 也可能是“name”(位于基“Person”中) //error C2385: 对“sex”的访问不明确 // 1> 可能是“sex”(位于基“Person”中) // 1> 也可能是“sex”(位于基“Person”中) // error C2385: 对“age”的访问不明确 // 1> 可能是“age”(位于基“Person”中) // 1> 也可能是“age”(位于基“Person”中) // #include <fstream> #include <iostream> #include <string> class Person { public: Person(); Person(std::string thename,char thesex,int theage); virtual void writein_tofile(); protected: std::string name; char sex; int age; }; //function of class person Person::Person() { } Person::Person(std::string thename,char thesex,int theage) { name=thename; sex=thesex; age=theage; } void Person::writein_tofile() { std::ofstream out; out.open("person.txt",std::ofstream::out|std::ofstream::app); out<<name<<" "<<sex<<" "<<age<<'\n'; out.close(); } class Student: public Person { public: Student(); Student(std::string thename, char thesex,int theage,std::string theIDstudent); void writein_tofile(); protected: std::string IDstudent; }; Student::Student() { ; } Student::Student(std::string thename, char thesex,int theage,std::string theIDstudent):Person(thename, thesex,theage)//在构造函数的 { IDstudent=theIDstudent; } void Student::writein_tofile() { std::ofstream out; out.open("student.txt",std::ofstream::out|std::ofstream::app); out<<name<<" "<<sex<<" "<<age<<" "<<IDstudent<<'\n'; out.close(); } class Teacher: public Person { public: Teacher(); Teacher(std::string thename, char thesex,int theage,std::string theIDteacher); void writein_tofile(); protected: std::string IDteacher; }; //function of class Teacher Teacher::Teacher() { ; } Teacher::Teacher(std::string thename, char thesex,int theage,std::string theIDteacher):Person(thename,thesex,theage) { IDteacher=theIDteacher; } void Teacher::writein_tofile() { std::ofstream out; out.open("teacher.txt",std::ofstream::out|std::ofstream::app); out<<name<<" "<<sex<<" "<<age<<" "<<IDteacher<<'\n'; out.close(); } class Teacher_student: public Student,public Teacher { public: Teacher_student(); Teacher_student(std::string thename, char thesex,int theage,std::string theIDstudent,std::string theIDclass,std::string theIDteacher); void writein_tofile(); protected: std::string IDclass; }; //function of class Teacher_student Teacher_student::Teacher_student() { ; } Teacher_student::Teacher_student(std::string thename, char thesex,int theage,std::string theIDstudent,std::string theIDclass,std::string theIDteacher) :Student(thename, thesex,theage,theIDstudent),Teacher(thename, thesex,theage,theIDteacher) { IDclass=theIDclass; } void Teacher_student::writein_tofile() { std::ofstream out; out.open("teather_student.txt",std::ofstream::out|std::ofstream::app); out<<name<<" "<<sex<<" "<<age<<" "<<IDstudent<<" "<<IDclass<<" "<<IDteacher<<'\n'; out.close(); } void readout_file(const char* file) { std::ifstream in; in.open(file,std::ifstream::in); std::string str; while (getline(in,str)) { std::cout<<str<<'\n'; } in.close(); }