首页 文章

GCC 4.1.1没有用tr1 :: unordered_map编译一些代码

提问于
浏览
1

我有以下代码从一个更大的程序测试一个概念,我尝试在Linux上使用GCC 4.1.1进行编译 . 由于公司环境的限制,我不能使用更新版本的编译器,因此我必须编译并使用当前可用的编译器版本 .

// test.cpp - my tr1::unordered_map usage example
#include <iostream>
#include <tr1/unordered_map>

namespace YY {

class X { public: X(int z_val = 0); private: int z; };
inline X::X(int z_val) : z(z_val) {}

enum XTE { Xt1, Xt2, Xt3 };

}

namespace std { namespace tr1 {

#define _my_tr1_hashtable_define_trivial_hash(T)        \
  template<>                                            \
    struct hash<T>                                      \
    : public std::unary_function<T, std::size_t>        \
    {                                                   \
      std::size_t                                       \
      operator()(T val) const                           \
      { return static_cast<std::size_t>(val); }         \
    }                                                     

_my_tr1_hashtable_define_trivial_hash(YY::XTE);

#undef _my_tr1_hashtable_define_trivial_hash
}}


namespace YY {
typedef std::tr1::unordered_map<long long, X*> TXM;
typedef std::tr1::unordered_map<XTE, TXM> TTXM;
}

int main()
{
    YY::TTXM m;
    std::cout << m.size();
    return 0;
}

然后我尝试编译这段代码,gcc给了我以下错误:

$ g++ -c test.cpp
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable: In instantiation of Б─≤Internal::hash_code_base, Internal::extract1st >, std::equal_to, std::tr1::hash, Internal::mod_range_hashing, Internal::default_ranged_hash, false>Б─≥:
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable:1014:   instantiated from Б─≤std::tr1::hashtable, std::allocator >, Internal::extract1st >, std::equal_to, std::tr1::hash, Internal::mod_range_hashing, Internal::default_ranged_hash, Internal::prime_rehash_policy, false, false, true>Б─≥
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/unordered_map:63:   instantiated from Б─≤std::tr1::unordered_map, std::equal_to, std::allocator >, false>Б─≥
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/bits/stl_pair.h:74:   instantiated from Б─≤std::pair, std::equal_to, std::allocator >, false> >Б─≥
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable:413:   instantiated from Б─≤Internal::extract1st, std::equal_to, std::allocator >, false> > >Б─≥
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable:861:   instantiated from Б─≤Internal::hash_code_base, std::equal_to, std::allocator >, false> >, Internal::extract1st, std::equal_to, std::allocator >, false> > >, std::equal_to, std::tr1::hash, Internal::mod_range_hashing, Internal::default_ranged_hash, false>Б─≥
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable:1014:   instantiated from Б─≤std::tr1::hashtable, std::equal_to, std::allocator >, false> >, std::allocator, std::equal_to, std::allocator >, false> > >, Internal::extract1st, std::equal_to, std::allocator >, false> > >, std::equal_to, std::tr1::hash, Internal::mod_range_hashing, Internal::default_ranged_hash, Internal::prime_rehash_policy, false, false, true>Б─≥
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/unordered_map:63:   instantiated from Б─≤std::tr1::unordered_map, std::equal_to, std::allocator >, false>, std::tr1::hash, std::equal_to, std::allocator, std::equal_to, std::allocator >, false> > >, false>Б─≥
test.cpp:42:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable:863: error: Б─≤Internal::hash_code_base::m_h1Б─≥ has incomplete type
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/functional:1101: error: declaration of Б─≤struct std::tr1::hashБ─≥

test.cpp:42:从这里实例化

YY::TTXM m;

如果我改变

typedef std::tr1::unordered_map<XTE, TXM> TTXM;

typedef std::tr1::unordered_map<XTE, TXM*> TTXM;

它成功编译,但这不是我想要做的 . 任何想法,建议,如何使这项工作?

1 回答

  • 3

    它's complaining that it doesn'具有 long long 的哈希函数,所以只需添加

    _my_tr1_hashtable_define_trivial_hash(long long);
    

    你应该好好去 .


    附:我认为使用TXM *而不是TXM工作的原因是因为编译器不必解析TXM类型,如果它是一个指针,所以它不会走下去找出它没有它的一切需要构造那种类型..即长long散列函数 . 如果你以后尝试创建一个TXM对象的实例,它会向你抱怨,然后你会遇到与你现在看到的类似的错误 .

相关问题