I have implement to class in C++, std c++11, the code two test winstrument class works well, but the code for station.cpp doesn't work, it fails in
stationA.insertInstrument(instrumentC);
but before this I insert more two instruments, the internal container is a vector and I use the method push_back to store the instrument:
void WStation::insertInstrument(const WInstrument& instrument)
{
_instruments.push_back(instrument);
}
This is the prototype of the class
class WInstrument
{
public:
WInstrument(int serialNumber, std::string description, Type type);
~WInstrument();
bool operator==(const WInstrument& rhs) const;
bool operator!=(const WInstrument& rhs) const;
double read() const;
std::string getDescription() const;
int getSerialNumber() const;
Type getType() const;
private:
int _serialNumber = -1;
Type _type;
std::string _description;
private:
std::uniform_real_distribution<double> *_dist;
};
This is the default constructor:
WInstrument::WInstrument(int serialNumber, std::string description, Type type):
_serialNumber(serialNumber),
_type(type),
_description(description)
{
switch(_type)
{
case wind:
{
_dist = new std::uniform_real_distribution<double>(0.0, 200.0);
break;
}
case temperature:
{
_dist = new std::uniform_real_distribution<double>(-20.0, 100.0);
break;
}
case humidity:
{
_dist = new std::uniform_real_distribution<double>(0.0, 100.0);
break;
}
}
}
I belive the problem is *_dist, because if I don't use pointer and implement direct, where I will use the function read, the code works. In the destructor I use delete _dist. The question is that I don't understand the problem.
The complete code is avaliable in https://github.com/retiarus/test-winstrument.