2017-03-22 4 views
0

개체 배열이있어서 개체의 다른 개체에 액세스하는 방법을 알 수 없습니다.개체 배열의 개체에 액세스하는 데 문제가 있습니다.

이 경우 객체 배열은 ArrayExam입니다. ExamRoom 클래스의 객체에 액세스하려고합니다.

나는 ArrayExam[0]->Doc = new Doctor();을 사용해 보았지만 여전히 운이 없다.

아이디어가 있으십니까? 헤더

#include"Header.h" 
#include"IOheader.h" 

Person::Person() 
{ 
    name = ""; 
    MEDcode = ""; 
} 

Person::Person(string sName, string sCode) 
{ 
    name = sName; 
    MEDcode = sCode; 
} 

void Person::SetName(string sName) 
{ 
    name = sName; 
} 

string Person::ReturnName() 
{ 
    return name; 
} 

string Person::ReturnMEDcode() 
{ 
    return MEDcode; 
} 

void Doctor::SetRoom(int iRoom) 
{ 
    room = iRoom; 
} 

int Doctor::ReturnRoom() 
{ 
    return room; 

} 

ExamRoom::ExamRoom() 
{ 

} 

Hospital::Hospital() 
{ 


    for (int i = 0; i < 50; i++) 
    { 
     ArrayExam[i]=new ExamRoom(); 
    } 
} 
void Hospital::Test() 
{ 
    ExamRoom A(); 

} 
+1

ArrayExam은 개체의 배열이 아니며 포인터의 배열입니다. 유스 케이스에서 객체 배열 (또는 벡터)을 사용할 수 있다면 더 나은 선택이 될 것입니다. –

답변

0

보호 ExamRoomDoc로의 회원

헤더

#include"IOheader.h" 

class Person 
{ 
protected: 
    string name; 
    string MEDcode; 
public: 
    Person(); 
    Person(string sName,string sCode); 
    void SetName(string sName); 
    string ReturnName(); 
    string ReturnMEDcode(); 

}; 


class Doctor : public Person 
{ 
private: 
    int room; 

public: 
    Doctor() :Person() { room = 0; }; 
    Doctor(string sName, string sCode, int iRoom) : Person(sName, sCode) { room = iRoom; }; 
    void SetRoom(int iRoom); 
    int ReturnRoom(); 

}; 

class Patient :public Person 
{ 
private : 
    string EMcode; 
    int age; 
public: 
    Patient() :Person() { EMcode = ""; age = 0; }; 
    Patient(string sName, string sCode,int iAge, string sEMcode) : Person(sName, sCode) { age = iAge; EMcode = sEMcode;}; 
}; 

class Child :public Patient 
{ 
public: 
    Child() :Patient() {}; 
    Child(string sName, string sCode, int iAge, string sEMcode) : Patient(sName,sCode,iAge,sEMcode) {}; 
}; 

class ExamRoom 
{ 
protected: 
    Person *Doc; 
    Person *Pat; 
    queue <WaitRoom> WaitQue; 
public: 
    ExamRoom(); 

}; 

class WaitRoom 
{ 
private: 
    Person *Wait; 
public: 
    WaitRoom() {}; 
}; 

class Hospital 
{ 
private: 
    ExamRoom *ArrayExam[50]; 
public: 
    Hospital(); 
    void Test(); 
}; 

기능. 그것이 당신이 그들에게 접근 할 수없는 이유입니다. ArrayExam[0]->Doc과 같이 직접 액세스하려면 회원을 공개로 설정해야합니다. 또는 멤버 변수를 간접적으로 설정하는 "setter"메서드를 만들 수 있습니다. 예를 들어, Doc를 들어, 당신은 그들이 더 이상 필요 후 수동으로 같은 ExamRoom의 인스턴스로 개체에 대한 공간을 할당하기 때문에, 당신은 동적으로 생성되는 모든 개체를 무료로 확인 안

SetDoc(Person* doc) 
{ 
    this->Doc = doc; 
} 

방법을 만들 수 있습니다 .

+0

정말 고마워요! – Quinn

관련 문제