2

I am trying to use an std::array as a key for a QHash class:

QHash<std::array<char, 6>, QString> _hash;

As required, I have overloaded a qHash function:

inline uint qHash(const std::array<char, 6> &key, uint seed)
{
    return ::qHashBits(key.data(), key.size(), seed);
}

An == operator is supported by std::array itself. But when compiling I get the error "no matching function for call to qHash(const std::array<char, 6>&)".

The same situation is with std::tuple. Although std::pair works fine. So do regular structs.

What am I doing wrong?


UPD

I tried also one-argument form of qHash:

inline uint qHash(const std::array<char, 6> &key)
{
    return ::qHashBits(key.data(), key.size(), 0);
}

Exactly the same result.


UPD2

Here is an example main.cpp file. No other code in the project.

#include <array>
#include <QHash>

static QHash<std::array<char, 6>, QString> hashByArray;

uint qHash(const std::array<char, 6>& key)
{
    return 0;
}

int main(int argc, char *argv[])
{
    hashByArray.insert({ 0, 0, 0, 0, 0, 0 }, "Hello");
    return 0;
}
Sergey
  • 21
  • 3
  • Like I said - move the declaration of `qHash` before `hashByArray`. `qHash` is not yet visible where it's needed. – Igor Tandetnik Sep 22 '19 at 13:46
  • 3
    The declaration of `qHash()` function for your data type must be visible before `#include ` which includes `"qhash"` and finally ``. In `qhashfunctions.h` the template is defined which matches your data type and calls the according `qHash()` function. – Mathias Schmid Sep 22 '19 at 15:34
  • 1
    Moving `qHash(...)` just before `hashByArray` has made no effect. Moving `qHash(...)` before `` has made it compile. Igor, Mathias, thank you very much. – Sergey Sep 23 '19 at 04:34
  • Possible related to [this question](https://stackoverflow.com/questions/59910341/how-to-use-stdstring-as-key-of-qhash/59910342#59910342): does placing `qHash` inside the `std` namespace resolve your issue? I'm able to include `QHash` before the `qHash` overload is defined. – m7913d Jan 25 '20 at 15:00
  • m7917d, sorry for the late answer. I have resolved this issue by tedious surfing through the project and rearranging includes in order to achieve the working state. And every change later made a pain. Thank you for the link. Going to try it next time I will use qHash. – Sergey Sep 08 '20 at 04:52

0 Answers0