views:

95

answers:

2

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.

+2  A: 

Why do you have two implementations of operator<? Keep any one.

Also, you'd probably need to define a proper hash function (and additionally specify which op< to be used).

dirkgently
I originally had only <(const TimeParameter) but then it complained I lacked the second. When I changed it to the second it complained I lacked the first. Hence both are in. As to the hash, I think I'll play around a bit to see if that's the culprit. You're absolutely right that I need a real hash function. +1 to you sir.
wheaties
A: 

This is the problem with rolling your own DateTime function. Not only has it been done ad nauseum but it is also a constant source of errors. I'd like to blame the project requirements but who am I really kidding? I didn't make the check of equals in the less than operators. So small, so stupid, and now, so very public.

wheaties