我有一个 C 类,它有一个string* ps私有数据成员。
现在,我想要一个unordered_map<C, int>需要自定义哈希函数的函数。
根据 c++ 参考,我可以这样做
namespace std {
template<>
class hash<C> {
public:
size_t operator()(const C &c) const
{
return std::hash<std::string>()(*c.ps);
}
};
}
operator()问题是我似乎无法结交C朋友以便我可以访问ps.
我试过这个:
class C;
template<>
class std::hash<C>;
class C{
//...
friend std::hash<C>::operator ()(const C&) const; // error: Incomplete type
};
// define hash<C> here.
但它说 Incomplete type ... in nested name specifier ...
我也无法扭转定义,因为如果稍后定义了 C 类,hash<C>则无法知道ps.
我在这里做错了什么?这种情况如何在不ps公开的情况下解决?