-2

I am trying to make a hash table that takes on a Vector2 as its key and an int as its value, but when I try to insert a key and value pair in its insert function I get an error:

no matching function for call to std::pair<Vector2, int>::pair(const Vector2&, int).

If anyone knows why this is happening and how to fix it, please help.

void HashTable::insert(const Vector2& key, int value)
{
    int index = hashFunction(key) % m_buckets.size();
    m_buckets.insert(m_buckets.begin() + index, std::make_pair(key, value));
}

I tried to use a different way to insert that is:

m_buckets[index].push_back(make_pair(key, value)

but it only gives me another error:

struct std::pair<Vector2, int> has no member named push.back

Vector2 is a vector class that takes to integers x and y.

std::vector<std::pair<Vector2, int>> m_buckets
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115

1 Answers1

1

It seems that your Vector2 class does not provide a copy constructor. Without it, I got the very same error message.

Live demo: https://godbolt.org/z/ca7r96roe

As for the second problem, you are trying to push_back into a vector element, which type is std::pair. std::pair does not have a push_back member function. Maybe, you wanted something as:

m_buckets[index] = std::make_pair(key, value);
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93