我有以下代码来测试一个更大程序的一个概念,我尝试在 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 测试.cpp /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable: Б─≤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:从Б─≤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,真>Б─≥ /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/unordered_map:63:从Б─≤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:从Б─≤实例化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: 实例化自Б─≤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: 实例化自Б─≤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:从Б─≤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:从Б─≤std实例化: :tr1::unordered_map, std::equal_to, std::allocator >, false>, std::tr1::hash, std::equal_to, std::allocator, std::equal_to, std::allocator >, false > > >,假>Б─≥ test.cpp:42:从这里实例化 /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable:863:错误:Б─≤内部: :hash_code_base::m_h1Б─≥ 类型不完整 /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/functional:1101:错误:Б─≤声明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;
它编译成功,但这不是我想要做的。任何想法,建议,如何使这项工作?