【问题标题】:Error C2888 migrating from VC9 to VC10从 VC9 迁移到 VC10 时出现错误 C2888
【发布时间】:2010-12-28 13:58:49
【问题描述】:

我正在尝试在 MSVC++ 10 上编译在 MSVC++ 9 中运行良好的解决方案和项目,但我遇到了问题,主要收到以下消息:

error C2888: 'std::hash' : 符号不能在命名空间'tr1'中定义

关于以下代码:

namespace std {
namespace tr1 {

template <>
struct hash< Rubedo::eChannelFamily >
{
    std::size_t operator()( const Rubedo::eChannelFamily& Key ) const
    {
        return ( int ) Key;
    }
};
}}

如果我能做到以下之一,我会非常高兴:

  • 修改代码修复bug,编译干净;
  • 强制编译器的行为类似于 MSVC++ 9.0。

我该怎么做呢? 非常感谢您。

【问题讨论】:

    标签: c++ visual-studio-2008 visual-studio-2010 visual-c++ migration


    【解决方案1】:

    hash 在 VS2010 中位于命名空间 std 中,因为它是 C++0x 标准库的一部分,而不是 std::tr1。只需删除 tr1 部分,编译器应该没问题。

    template<> class std::hash< Rubedo::eChannelFamily >>
        : public std::unary_function<const Rubedo::eChannelFamily, size_t>
    {
    public:
        size_t operator()(const Rubedo::eChannelFamily& ref) const {
            return ( int ) ref;
        }
    };
    

    这是对我自己的类型的哈希的相当微不足道的修改,它可以成功编译。

    【讨论】:

      【解决方案2】:

      你必须像这样继承unary_function,并且不再需要tr1

      namespace std 
       {
             template <>
             struct hash<Rubedo::eChannelFamily> : public unary_function<Rubedo::eChannelFamily, size_t>
             {
                   size_t operator()(const Rubedo::eChannelFamily& key) const
                   {
                         return (size_t) key;
                   }
            };
       }
      

      【讨论】:

      • 你知道,他在他的 OP 中指定了 CustomType 和 //你的代码。
      • @DeadMG:是的。我用他的类型和代码替换了 YourCustomType。 :-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多