I have a class C, which has a string* ps
private data member.
Now, I'd like to have an unordered_map<C, int>
for which I need a custom hash function.
According to the c++ reference, I can do that like
namespace std {
template<>
class hash<C> {
public:
size_t operator()(const C &c) const
{
return std::hash<std::string>()(*c.ps);
}
};
}
The problem is that I can't seem to make operator()
and C
friends so that I could access ps
.
I have tried this:
class C;
template<>
class std::hash<C>;
class C{
//...
friend std::hash<C>::operator ()(const C&) const; // error: Incomplete type
};
// define hash<C> here.
but it says that Incomplete type ... in nested name specifier ...
I can't turn around the definitions either, because if class C is defined later, the hash<C>
has no way to know about ps
.
What am I doing wrong here? How can this situation be fixed without making ps
public?