繁体   English   中英

在tr1中为模板类定义哈希

[英]defining hash in tr1 for a template class

我有网关课程。 我需要将此类的对象存储在哈希表中(使用tr1 :: unordered_set)。 我在另一个上下文中都使用了unorderd_set和网关类,它们都可以正常工作,但是由于无法在tr1的命名空间中定义其hash函数,所以我无法弄清楚如何将网关放入无序集合中。

我尝试过:(以及许多其他变体)

namespace std {
    namespace tr1 {
        template<> <typename T> inline size_t hash<(typename Gateway<T>)>::operator()(Gateway<T> gT) const {
            return gT.getCore()->hash();   //!DOESN't WORK
    }
}

编译器说this( typename Gateway<T> )是错误的。 如果我走()关闭时,它假定该>>中的端部hash<typename Gateway<T>>()是输出流。

过去我做过

namespace std {
    namespace tr1 {
        template<> inline size_t hash<Board>::operator()(Board b) const {
            return b.hash();            //!WORKS FINE
        }
    }
}

谁能阐明这个问题?

更新资料

感谢您提供答案,但是仍然存在问题编译器说无效使用了不完整类型struct std::tr1::hash<>

当我们使用没有完整定义的类,但在声明之前完全定义了类时,会发生这种错误。 我已经以没有问题的非常相似的方式在没有模板的情况下使用了它。

template <typename T> 
inline size_t hash<(typename Gateway<T>)>::operator()(Gateway<T> gT) const 

语法错误。 typename不是必需的。

正确的语法是这样的:

namespace tr1
{
    template <typename T> 
    inline size_t hash<Gateway<T> >::operator()(const Gateway<T> & gT) const 
    {
          return gT.getCore()->hash();          
    }
}

注意,我还设置了参数const Gateway<T> & type,只是为了避免复制!

还要注意,名称空间tr1不是嵌套的名称空间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM