-2

I have two classes, HouseSecurity and HomeOwner. One of the data members of HomeOwner is a pointer to House Security. In my HomeOwner method I'm trying to call a function from HouseSecurity via the pointer, but I can't find a way that I can call it. The line currentSensorState = house.getSensorState() is the one causing me problems. Any ideas? Thanks!

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();
}
CBC
  • 1
  • Recommended read: https://stackoverflow.com/questions/46991224/are-there-any-valid-use-cases-to-use-new-and-delete-raw-pointers-or-c-style-arr – user0042 Dec 08 '17 at 17:40
  • You need to do `house->getSensorStatus()` – Jake Freeman Dec 08 '17 at 17:45
  • `house` is a pointer, so you need to dereference the pointer, either `(*house).getSensorStatus();` or `house->getSensorStatus();`. And perhaps take it easy on virtual inheritance until you have figured out pointers. – Bo Persson Dec 08 '17 at 17:45
  • Perfect, thanks! The house->getSensorStatus worked a dream :) – CBC Dec 08 '17 at 17:49

1 Answers1

0

When trying to access members of pointer, the pointer needs to be dereferenced. The shortcut is to use the -> operator as in:

house->getSensorStatus();

Eyal Cinamon
  • 939
  • 5
  • 16