【问题标题】:Static with classes error静态类错误
【发布时间】:2016-11-01 03:35:55
【问题描述】:

这是它的 C++。

我真的不知道问题出在哪里,因为两个静态演员的类型相同,但它只正确显示员工信息,当涉及到患者信息时,它只显示日期和日期,但没有人员信息,(员工确实正确打印人员信息)

  void *print( ){
        Node *aux;
        aux = this->head;
        while(aux){
            Employee *employee = static_cast<Employee*>(aux->getPerson());
            Patient *patient = static_cast<Patient*>(aux->getPerson());
            if(employee) {
                employee->info();
            }
            else if (patient){
                patient->info();
                //Should be one of the cases above
            }
            aux = aux->getNext();
        }
        return 0;

就在我打印这个的那一刻,它只打印员工信息,但不显示患者信息。

class Patient: public Person {

private:
int Id_Patient;
Person person;
string Date_In;
string Date_Out;

public:
    Patient(){
        this->Date_In;
        this->Date_Out;
    }
    Patient (int Id_Patient, Person person){
        this->Id_Patient=Id_Patient;
        this->person=person;
    }
    Patient (int Id_Patient, Person person, string Date_In, string Date_Out){
        this->Id_Patient=Id_Patient;
        this->Date_Out=Date_Out;
        this->Date_In=Date_In;
    }
    void setId_Patient(int Id_Patient){
        this->Id_Patient=Id_Patient;}
    int getId_Patient(){
        return this->Id_Patient;}

    void setDate_In(string Date_In){
        this->Date_In=Date_In;}
    string getDate_In(){
        return this->Date_In;}

    void setDate_Out(string Date_Out){
        this->Date_Out=Date_Out;}
    string getDate_Out(){
        return this->Date_Out;} 

    void setPerson(Person person) {
        this->person=person;    }       
    Person getPerson() {
        return this->person;}       

    void info() {
        cout <<"=================================" << endl; 
        cout << "Patient ID: " << this->Id_Patient << endl;
        this->person.info();
        cout << "Date_In: " << this->Date_In << endl;
        cout << "Date_Out: "<< this->Date_Out << endl;
        cout <<"=================================" << endl; 
    }               

}; //分类病人

class Employee: public Person {
private:
    int Employee_Code;
    Person person;
    double Salary;
public:
    Employee() {
        this->Employee_Code;
        this->Salary;}

    Employee(int Employee_Code, Person person){
        this->Employee_Code=Employee_Code;
        this->person=person;
    }
    Employee(int Employee_Code, double Salary){
        this->Employee_Code=Employee_Code;
        this->Salary=Salary;
    }
    Employee(int Employee_Code, Person person, double Salary){
        this->Employee_Code=Employee_Code;
        this->person=person;
        this->Salary=Salary;
    }   
    void setEmployee_Code(int Employee_Code){
        this->Employee_Code=Employee_Code;}
    int getEmployee_Code(){
        return this->Employee_Code;}

    void setSalary(double Salary){
        this->Salary=Salary;}
    double getSalary(){
        return this->Salary;}

    void setPerson(Person person) {
        this->person=person;    }       
    Person getPerson() {
        return this->person;}   

    void info() {
        cout <<"=================================" << endl; 
        cout << "Employee_Code: " << this->Employee_Code << endl;
        this->person.info();
        cout << "Salary: " << this->Salary << endl;
        cout <<"=================================" << endl; 
    }

}; //分类员工

【问题讨论】:

    标签: c++ oop polymorphism


    【解决方案1】:

    看来 OP 将 static_castdynamic_cast 混淆了。但是有一个更好的方法可以消除任何强制转换的需要:将纯虚方法 virtual void info()=0; 添加到 Person

    PatientEmployeeinfo 将实现Personinfoprint 可以简单

    aux->getPerson()->info();
    

    全部完成。

    题外话,从print 返回void * 有什么意义? void * 是想象力的失败、糟糕的计划或与 C++ 中的 C 库的接口。你几乎不应该使用它,当然也不应该return 0

    【讨论】:

    • 非常感谢,虚拟无效的人解决了这个问题,我知道我需要退货,但我删除了它,它工作得很好。另外,尝试了 aux->getPerson()->info();并且它编译错误,[Error] cannot dynamic_cast 'aux->Node::getPerson()->Person::info()' (of type 'void') to type 'class Employee*' (source is not a pointer)并且 [Error] void 值没有被忽略,因为它应该是 @user4581301
    • 不完全确定所有评论的含义,@EduardoGonzalez。我认为这意味着您是Employee *employee = dynamic_cast&lt;Employee*&gt;(aux-&gt;getPerson()-&gt;info());,这不是必需的。整个while 循环变为:while(aux){ aux-&gt;getPerson()-&gt;info(); aux = aux-&gt;getNext(); }
    猜你喜欢
    • 1970-01-01
    • 2017-04-23
    • 2012-02-26
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 2011-09-04
    相关资源
    最近更新 更多