I've done something wrong in defining my class which is causing Microsoft's implementation of the hash_multimap to "miss." Here is my class:
class TimeParameter {
public:
TimeParameter(int _year, int _julianDay, int _hour) : m_Year(_year),
m_JulianDay(_julianDay),
m_Hour(_hour){}
int GetHour() const {return m_Hour;}
int GetJulianDay() const {return m_JulianDay;}
int GetYear() const {return m_Year;}
bool operator==(const TimeParameter &_rhs);
bool operator < (const TimeParameter &_rhs);
operator size_t() const;
friend bool operator<(const TimeParameter &_lhs, const TimeParameter &_rhs);
private:
int m_Hour,
m_JulianDay,
m_Year;
};
with the cpp file as
TimeParameter::operator size_t() const
{
return (size_t)(8765u * (m_Year % 6)) + (size_t)(m_JulianDay*24u) + (size_t)m_Hour;
}
bool operator<(const TimeParameter &_lhs, const TimeParameter &_rhs)
{
if( _lhs.GetYear() > _rhs.GetYear() )
{
return false;
}
else if( _lhs.GetYear() == _rhs.GetYear()
&& _lhs.GetJulianDay() > _rhs.GetJulianDay() )
{
return false;
}
else if( _lhs.GetYear() == _rhs.GetYear()
&& _lhs.GetJulianDay() == _rhs.GetJulianDay()
&& _lhs.GetHour() > _rhs.GetHour() )
{
return false;
}
return true;
}
bool TimeParameter::operator==(const TimeParameter &_rhs)
{
return m_Hour == _rhs.GetHour()
&& m_JulianDay == _rhs.GetJulianDay()
&& m_Year == _rhs.GetYear();
}
bool TimeParameter::operator <(const TimeParameter &_rhs)
{
if( m_Year > _rhs.GetYear() )
{
return false;
}
else if( m_Year == _rhs.GetYear()
&& m_JulianDay > _rhs.GetJulianDay() )
{
return false;
}
else if( m_Year == _rhs.GetYear()
&& m_JulianDay == _rhs.GetJulianDay()
&& m_Hour > _rhs.GetHour() )
{
return false;
}
return true;
}
If I then use this class as a key in a hash_multimap<'TimeParameter,Foo'> I can't then use hash_multimap::find() to find anything. What did I do wrong?
Note: Yes, I realize this is yet another datetime style class (my wheel is square!) We have to create a class for every row of every DB table as project requirements. And yes, they really did create a table just for year, day, hour instead of using the well established DateTime type columns. Since they're paying us, we're not arguing.
Also, feel free to harp on me for violating DRY.
Update:
Changing the size_t() operator to return 0;
did not solve the problem. It still can't find anything. Changing the operators to always yield true also did not solve the problem. There must be something I've not done right somewhere else in the program to cause such an error.