Nov 4, 2017 at 2:25am UTC
here is a picture of the actual error and exception throw error i'm getting.
https://cdn.discordapp.com/attachments/370147666673532940/376183527466729472/9.png
but essentially, i get an exception thrown right as i try to get my temperature
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#ifndef weatherData_h
#define weatherData_h
#include <list>
#include "observer.h"
#include "subject.h"
class weatherData : public subject {
public :
weatherData()
: weatherData(0.0, 0.0, 0.0) {}
weatherData(double temp, double humidity, double pressure)
: observers_(), temp_(temp), humidity_(0.0), pressure_(pressure) {}
void registerObserver(observer* o) {
observers_.push_front(o);
}
void removeObserver(observer* o) {
auto it = find(observers_.begin(), observers_.end(), o);
if (it != observers_.end()) {
observers_.erase(it);
}
}
void notifyObservers() const {
for (auto o : observers_)
{
o->update();
}
}
double temp() const {
return temp_; //here is where i get an error
}
double humidity() const {
return humidity_;
}
double pressure() const {
return pressure_;
}
void setMeasurements(double temp, double humidity, double pressure) {
temp_ = temp;
humidity_ = humidity;
pressure_ = pressure;
measurementsChanged();
}
void measurementsChanged() const { notifyObservers(); }
//-------------
private :
std::list<observer*> observers_;
double temp_;
double humidity_;
double pressure_;
};
#endif /* weatherData_h */
here is subject.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef subject_h
#define subject_h
#include "observer.h"
//----------------------------------------------------------------------
class subject {
public :
virtual void registerObserver(observer* o) = 0; // Interface == *all* pure virtual methods
virtual void removeObserver(observer* o) = 0;
virtual void notifyObservers() const = 0;
};
#endif /* subject_h */
and here is observer.h
1 2 3 4 5 6 7 8 9 10 11
#ifndef observer_h
#define observer_h
//----------------------------------------------------------------------
class observer {
public :
virtual void update() = 0;
};
#endif /* observer_h */
Last edited on Nov 4, 2017 at 2:28am UTC
Nov 4, 2017 at 2:35am UTC
i get that if i make a pointer point to null, it will give me null. but i can't find in my code where the pointer points to null
(that's a lot of points)
Last edited on Nov 4, 2017 at 2:35am UTC
Nov 4, 2017 at 2:43am UTC
okay, i think i understood that.. sorry i'm still a beginner in OOP, how would i resolve this problem?
Nov 4, 2017 at 2:52am UTC
You should either add a check to see if the casted pointer is null before trying to use it, or if the object should have been of the type you specified with dynamic_cast, you should investigate why you got an object of the wrong type. The debugger can tell you the type of the object pointed to by subject_. See where you're creating objects of that type.
Nov 6, 2017 at 10:31pm UTC
hello!
sorry for the late update but it turned out that I didn't define my pointer within the constructor and thus my compiler automatically made it point to nullptr! I appreciate the help!