2つのオブジェクトのunordered_mapを含むクラススケジューラがあります。C++ unordered_mapハッシュ関数がオブジェクトを見つけることができません
class Scheduler
{
public:
...
private:
unordered_map<Time, Activity> schedule;
}
私はエラーを取得する: 'リスト反復子dereferenceableない' - オブジェクトがここで見つからなかったことを意味している。ここで
void appoint(Time &time, const string activity)
{
Time hashTime(time + incrementor);
schedule.find(hashTime)->second.active = 1; // <-- here
}
は、私のハッシュツールです:ここ
namespace std {
// does get here
template <>
struct hash<Time>
{
std::size_t operator()(const Time& k) const
{
return ((hash<short>()(k.hr)
^(hash<short>()(k.min) << 1)) >> 1)
^(hash<bool>()(k.morning) << 1);
}
};
}
struct Time {
short hr, min;
bool morning;
// doesnt get here
bool operator==(const Time &other) const
{
return (hr == other.hr && min == other.min
&& morning == other.morning);
}
}
unordered_mapに存在する証拠です。
私はit == schedule.end()
が真実であることを確認しました。
auto it = schedule.find(hashTime);
if (it == schedule.end()) {
cout << "ok";
}
// this does manage to get found
schedule.insert(make_pair(Time(9, 30, 1), Activity(0,"")));
auto it2 = schedule.find(hashTime);
if (it2 == schedule.end()) {
cout << "ok";
}
どういうわけか私の地図の最初の構成が間違っています。
Scheduler()
{
Time start(12,0,1); // 12:00am
Time incrementor; // 00:00
Time static_incrementor(0, 15); // 00:15
// creates an empty schedule
// there are 96, 15 min intervals from 12:00am to 11:45pm
for (int i = 0; i < 48; i++) {
Time newTime(start + incrementor);
newTime.morning = true;
cout << newTime.hr << ":" << newTime.min << newTime.morning << endl;
schedule.insert(make_pair(newTime, Activity(0, "")));
incrementor += static_incrementor;
}
Time start2(12, 0, 0); // 12:00pm
Time incrementor2; // 00:00
for (int i = 0; i < 48; i++) {
Time newTime(start2 + incrementor2);
newTime.morning = false;
cout << newTime.hr << ":" << newTime.min << newTime.morning << endl;
schedule.insert(make_pair(newTime, Activity(0, "")));
incrementor2 += static_incrementor;
}
};
あなたの '演算子==()'は 'Time'のように見えますか?私たちに[mcve]を表示するには、あなたの質問を編集する必要があります。特に、**最小**(問題を示す最小のサンプルプログラムに問題を減らす必要があります)と**完全**(プログラム全体を含める必要があります)に注意してください。 –
申し訳ありません。あなたは 'operator ==()'を持っています。すみません。 –
@WhozCraig:私たちのコメントはポストを越えました。 –