类的成员,是指向另一个类的指针

时间:2017-12-08 17:37:53

标签: c++ pointers

我有两个班,HouseSecurity和HomeOwner。 HomeOwner的一个数据成员是House Security的指针。在我的HomeOwner方法中,我试图通过指针调用HouseSecurity中的函数,但我找不到可以调用它的方法。行currentSensorState = house.getSensorState()是导致我问题的那一行。有任何想法吗? 谢谢!

class HouseSecurity : virtual public SecurityStatus{        
        SensorInfo mySensorStatus;      
public:
        SensorInfo getSensorStatus();       
        void setSensorStatus(SensorInfo);   
};

class HomeOwner : virtual public IObserver{
    SensorInfo currentSensorState;  // Current state of sensors
    string myName;                  // Personal information pf HomeOwner
    string myPhoneNumber;
    string myEmail;
    HouseSecurity *house;           // HouseSecurity pointer

public:
    HomeOwner (string, string, string, HouseSecurity *); // Constructor
    void Update();      // Called from Notify function in HouseSecurity class
};

// HomeOwner Method
// HomeOwner constructor
HomeOwner::HomeOwner(string name, string number, string email, HouseSecurity *home){
    myName = name;
    myPhoneNumber = number;
    myEmail = email;
    house = home;   // pointer to House Security in the system
}

// Checks the status of the sensors and updates each of the 
void HomeOwner::Update(){
    cout << "test update" << house << endl;
    currentSensorState = house.getSensorStatus();
    cout << "HomeOwner " << myName << " says that the status of windows 1, 2 and door are " << currentSensorState.getWin1State() << " " << currentSensorState.getWin2State() << " "
        << currentSensorState.getDoorState();
}

1 个答案:

答案 0 :(得分:0)

当尝试访问指针成员时,需要取消引用指针。快捷方式是使用 - &gt;运营商如:

House - 临近&GT; getSensorStatus();