Question

I have following code to test one concept from a bigger program, and I try to compile with GCC 4.1.1 on Linux. I cannot use newer version of compiler due to corporate environment restrictions and so I have to make it compiling and working with currently available compiler version.

// 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;
}

Then I try to compile this code, gcc gives me following error:

$ 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: instantiated from here is

YY::TTXM m;

If I change

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

into

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

it compiles succesfully, but this is not what I want to do. Any ideas, suggestions, how to make this work?

Was it helpful?

Solution

It's complaining that it doesn't have a hash function for long long, so just add

_my_tr1_hashtable_define_trivial_hash(long long);

and you should be good to go.


P.S. I think the reason why using TXM* instead of TXM works is because the compiler doesn't have to resolve the TXM type if it's a pointer, so it doesn't go down the path of figuring out that it doesn't have everything it needs to construct that type.. namely a long long hash function. If you were to later try to create an instance of a TXM object, it'd complain at you then with a similar error to what you're seeing now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top