我想爲我自己的散列函數編寫一個基本上是無符號整數封裝的類。我一直試圖按照這個線程here:和這個資源here。爲什麼這不起作用?查看代碼註釋以瞭解錯誤。我的散列鍵類型和函數有什麼問題?
struct Entity
{
unsigned int id;
bool operator==(const Entity &other) const
{
return (id == other.id);
}
};
template<struct T>
class EntityHash;
template<>
class EntityHash<Entity> // Error: Type name is not allowed
{
public:
std::size_t operator()(const Entity& entity) const
{
size_t h1 = std::hash<unsigned int>()(entity.id);
return h1; // Also... do I need a << 1 at the end of this to bitshift even though I'm not combining?
}
};
// Elsewhere
std::unordered_map<Entity, unsigned int, EntityHash> map; // Error: Argument list for class template EntityHash is missing
關於'<< 1'的事情:如果你需要它取決於你的要求。代碼在這兩種情況下都是正確的(當然,那部分) – deviantfan
@deviantfan謝謝,我想,因爲我沒有合併它。 – Stradigos