标题:类的继承错误 初学 请多指教
只看楼主
gwcome
Rank: 2
来 自:尼玛星球
等 级:论坛游民
帖 子:43
专家分:33
注 册:2013-1-22
结帖率:100%
已结贴  问题点数:20 回复次数:6 
类的继承错误 初学 请多指教
程序代码:
//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();

 }
搜索更多相关主题的帖子: 继承 teacher person 
2013-03-25 17:53
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6809
专家分:42393
注 册:2010-12-16
得分:20 
class Teacher_student: public Student,public Teacher
他继承了Student和Teacher ,那么它就有Student的name 和Teacher 的name了,

 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();
 }
你这个函数中访问 name,他不知道你访问的是Student::name 还是Teacher::name.


Teacher_student 是个什么对象,他为什么又是学生又是老师?而且有两个name 两个sex 两个age?好怪异的设计



我行我乐
我的博客:
http://blog.yuccn. net
2013-03-25 18:20
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6809
专家分:42393
注 册:2010-12-16
得分:0 
难道Teacher_student 是传说中的007?哈哈

如果你硬要这样设计,那么输出的时候加上(Student::) ==> out<<Student::name<<" "<<Student::sex<<" "<<Student::age<<" "<<IDstudent<<" "<<IDclass<<" "<<IDteacher<<'\n';
或者 Teache:: 就行了




我行我乐
我的博客:
http://blog.yuccn. net
2013-03-25 18:27
gwcome
Rank: 2
来 自:尼玛星球
等 级:论坛游民
帖 子:43
专家分:33
注 册:2013-1-22
得分:0 
回复 3楼 yuccn

我设计的是一个简单的信息管理程序,有学生、教师、助教。
版主的007从哪来~~哈哈~~

小菜小菜
2013-03-25 18:48
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6809
专家分:42393
注 册:2010-12-16
得分:0 
回复 4楼 gwcome
多重身份,有不同的名字可以理解,但是 一个人有不同的性别和年龄,不是特务(007)是什么?^_^

我行我乐
我的博客:
http://blog.yuccn. net
2013-03-25 18:52
gwcome
Rank: 2
来 自:尼玛星球
等 级:论坛游民
帖 子:43
专家分:33
注 册:2013-1-22
得分:0 
回复 5楼 yuccn
版主大哥,那要是多个身份,我给他们付同样的参数。那还是007???
没有栗子说个JB(糗百不知版主看吗?),上代码求版主大哥看TeachingStudent类的构造函数


程序代码:
#include <iostream>
#include <string>

class Person
{
public:
    Person(std::string theName);

    void introduce();

protected:
    std::string name;
};

class Teacher : public Person
{
public:
    Teacher(std::string theName, std::string theClass);

    void teach();
    void introduce();

protected:
    std::string classes;
};

class Student : public Person
{
public:
    Student(std::string theName, std::string theClass);

    void attendClass();
    void introduce();

protected:
    std::string classes;
};

class TeachingStudent : public Student, public Teacher
{
public:
    TeachingStudent(std::string theName, std::string classTeaching, std::string classAttending);

    void introduce();
};

Person::Person(std::string theName)
{
    name = theName;
}

void Person::introduce()
{
    std::cout << "大家好,我是" << name << "。\n\n";
}

Teacher::Teacher(std::string theName, std::string theClass) : Person(theName)
{
    classes = theClass;
}

void Teacher::teach()
{
    std::cout << name << "" << classes << "。\n\n";
}

void Teacher::introduce()
{
    std::cout << "大家好,我是" << name << ", 我教" << classes << "。\n\n";
}

Student::Student(std::string theName, std::string theClass) : Person(theName)
{
    classes = theClass;
}

void Student::attendClass()
{
    std::cout << name << "加入" << classes << "学习。\n\n";
}

void Student::introduce()
{
    std::cout << "大家好,我是" << name << ", 我在" << classes << "学习。\n\n";
}

TeachingStudent::TeachingStudent(std::string theName,
                                 std::string classTeaching,
                                 std::string classAttending)
                                 : Teacher(theName, classTeaching), Student(theName, classAttending)
{
}

void TeachingStudent::introduce()
{
    std::cout << "大家好,我是" << Student::name << "。我教" << Teacher::classes << ", ";
    std::cout << "同时我在" << Student::classes << "学习。\n\n";
}

int main()
{
    Teacher teacher("小甲鱼", "C++入门班");
    Student student("迷途羔羊", "C++入门班");
    TeachingStudent teachingStudent("丁丁", "C++入门班", "C++进阶班");

    teacher.introduce();
    teacher.teach();
    student.introduce();
    student.attendClass();
    teachingStudent.introduce();
    teachingStudent.teach();
    teachingStudent.attendClass();

    return 0;
}

小菜小菜
2013-03-25 20:47
gwcome
Rank: 2
来 自:尼玛星球
等 级:论坛游民
帖 子:43
专家分:33
注 册:2013-1-22
得分:0 
回复 5楼 yuccn
版主大哥风趣幽默  我明白了  太感谢了

小菜小菜
2013-03-25 20:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-401873-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.146164 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved