0

I've been trying to test out the hash table from the Bitsquid foundations library in tandem with the Bitsquid blog post on Entity Component Systems but the library changed since the blog post was written and I'm getting a type conversion error.

foundation::Hash < Entity > map;
struct Instance { int i; };
Instance MakeInstance(int index) { Instance inst = { index }; return inst; }
Instance Lookup(Entity e) { return MakeInstance(hash::get(map, e.id, 0)); }

Error: Cannot convert argument 1 from 'const Entity' to 'unsigned int'

Or sometimes I get: template parameter 'T' is ambiguous

If I change < Entity > to int, for example, the error goes away. When I tried a regular unordered map, it said the STL doesn't know how to hash to an Entity, so I'm guessing it's the same issue but the error is presenting differently in this library.

However, knowing the problem isn't the same thing as knowing how to fix it. I've tried casting to uint64_t and int but have had no success. What do I need to do here?

PS: Yes, I've seen the unit test.

Stradigos
  • 814
  • 1
  • 13
  • 29
  • The library looks like it's been written by a C programmer struggling to learn C++ (it uses non-member functions with the "object" to be operated on as first parameter, instead of proper OO with implicit `this`). Why are you wasting your time with it, given it's got a feral interface that's convoluted to use and poor/old documentation? – Tony Delroy Jun 24 '15 at 01:06
  • You say you tried `std::unordered_map` - why don't you look at some answers here about how to write a hash function for Entity? (If key is `int id;` field, `namespace std { template<> struct hash { std::size_t operator()(const Entity& e) const { return std::hash(e.id); } }; }`. – Tony Delroy Jun 24 '15 at 01:13
  • 3
    In all fairness to the library, it says in the "Library Design" section that it's pretty much trying not to be "proper OOP" for very specific reasons. Those reasons are similar to my reasons. I don't intend on staying with this library for long, but I don't really want to return to the standard library either. In any case, I had a similar message when using a unordered_map... it doesn't know how to hash Entity. Not sure how to get around that either. I'm new to hashing. – Stradigos Jun 24 '15 at 01:14
  • 1
    Very specific reasons that make very little sense. Anyway - you'll need two things to use your own class with `std::unordered_map` - a hash function (the code in my last comment), and a key comparison function ala `bool operator==(const Entity& lhs, const Entity& rhs) { return lhs.id == rhs.id; }`. If you have trouble, post your code and error messages. Some help/alternatives [here](http://stackoverflow.com/questions/17016175/c-unordered-map-using-a-custom-class-type-as-the-key) – Tony Delroy Jun 24 '15 at 01:24
  • 1
    Thanks for the example and extremely helpful link. I think I got it from here. – Stradigos Jun 24 '15 at 01:33

0 Answers0