Hey Everyone,
I am basically trying to create a sorted hash map using boost multiindex. I
am following the examples but I am unable to insert my object into the
container when I use a non-primitive key.
Is it possible to use a non-primitive key?
Any help or pointers would be appreciated.
Thanks
J.O.
#include
#include
#include
#include
#include
#include
using namespace ::boost;
using namespace ::boost::multi_index;
#include <iostream>
struct Key
{
uint32_t k1;
uint32_t k2;
Key(uint32_t k1, uint32_t k2) : k1(k1), k2(k2) {}
bool operator==(const Key &key) const
{
if (k1 != key.k1) return false;
if (k2 != key.k2) return false;
return true;
}
};
struct KeyEqual
{
bool operator()(const Key &key1, Key &key2) const
{
return key1 == key2;
}
};
struct KeyHash
{
std::size_t operator()(const Key& key)const
{
return boost::hash()(key.k1) +
boost::hash()(key.k2);
}
};
struct Data
{
Key key;
int value;
Data(const Key& key, int value): key(key), value(value) {}
bool operator<(const Data& data)const
{
return (value < data.value);
}
};
typedef multi_index_container<
Data,
indexed_by<
// sort by Data::operator<
ordered_unique,
// hashed on key
hashed_unique >
DataSet;
typedef DataSet::nth_index<1>::type HashIndex;
int main()
{
DataSet dataSet;
HashIndex &hashIndex = dataSet.get<1>();
Key newKey(1,2);
Data newData(newKey,3);
dataSet.insert(newData); // *** Error - See below ***
// find an Data by key
Data data=*(hashIndex.find(newKey,KeyHash(),KeyEqual()));
};
This is the error I get:
g++ -I../include -rdynamic -DCOREDUMP -DLINUX -pthread -pipe -Wall -W -g
-DDEBUG -c mIndex.cc -o mIndex.o
mIndex.cc: In member function ‘size_t boost::hash<T>::operator()(const T&)
const [with T = Key]’:
/usr/include/boost/multi_index/hashed_index.hpp:957: instantiated from
‘void boost::multi_index::detail::hashed_index::unchecked_rehash(size_t) [with KeyFromValue =
boost::multi_index::member, Hash = boost::hash<Key>,
Pred = std::equal_to<Key>, SuperMeta =
boost::multi_index::detail::nth_layer<2, Data,
boost::multi_index::indexed_by,
boost::multi_index::hashed_unique, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, std::allocator<Data> >, TagList = boost::mpl::vector0,
Category = boost::multi_index::detail::hashed_unique_tag]’
/usr/include/boost/multi_index/hashed_index.hpp:942: instantiated from
‘void boost::multi_index::detail::hashed_index::reserve(size_t) [with KeyFromValue =
boost::multi_index::member, Hash = boost::hash<Key>,
Pred = std::equal_to<Key>, SuperMeta =
boost::multi_index::detail::nth_layer<2, Data,
boost::multi_index::indexed_by,
boost::multi_index::hashed_unique, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, std::allocator<Data> >, TagList = boost::mpl::vector0,
Category = boost::multi_index::detail::hashed_unique_tag]’
/usr/include/boost/multi_index/hashed_index.hpp:629: instantiated from
‘boost::multi_index::detail::hashed_index_node<typename
SuperMeta::type::node_type>*
boost::multi_index::detail::hashed_index::insert_(typename boost::call_traits::param_type,
boost::multi_index::detail::hashed_index_node<typename
SuperMeta::type::node_type>*) [with KeyFromValue =
boost::multi_index::member, Hash = boost::hash<Key>,
Pred = std::equal_to<Key>, SuperMeta =
boost::multi_index::detail::nth_layer<2, Data,
boost::multi_index::indexed_by,
boost::multi_index::hashed_unique, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, std::allocator<Data> >, TagList = boost::mpl::vector0,
Category = boost::multi_index::detail::hashed_unique_tag]’
/usr/include/boost/multi_index/ordered_index.hpp:556: instantiated from
‘boost::multi_index::detail::ordered_index_node<typename
SuperMeta::type::node_type>*
boost::multi_index::detail::ordered_index::insert_(typename boost::call_traits::param_type,
boost::multi_index::detail::ordered_index_node<typename
SuperMeta::type::node_type>*) [with KeyFromValue =
boost::multi_index::identity<Data>, Compare = std::less<Data>, SuperMeta =
boost::multi_index::detail::nth_layer<1, Data,
boost::multi_index::indexed_by,
boost::multi_index::hashed_unique, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, std::allocator<Data> >, TagList = boost::mpl::vector0,
Category = boost::multi_index::detail::ordered_unique_tag]’
/usr/include/boost/multi_index_container.hpp:465: instantiated from
‘std::pair::type::node_type*, bool>
boost::multi_index::multi_index_container::insert_(const Value&) [with Value = Data, IndexSpecifierList =
boost::multi_index::indexed_by,
boost::multi_index::hashed_unique, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, Allocator = std::allocator<Data>]’
/usr/include/boost/multi_index/detail/index_base.hpp:148: instantiated
from ‘std::pair::type*, bool> boost::multi_index::detail::index_base::final_insert_(typename
boost::call_traits<Value>::param_type) [with Value = Data,
IndexSpecifierList =
boost::multi_index::indexed_by,
boost::multi_index::hashed_unique, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, Allocator = std::allocator<Data>]’
/usr/include/boost/multi_index/ordered_index.hpp:252: instantiated from
‘std::pair, bool>
boost::multi_index::detail::ordered_index::insert(typename boost::call_traits::param_type) [with KeyFromValue =
boost::multi_index::identity<Data>, Compare = std::less<Data>, SuperMeta =
boost::multi_index::detail::nth_layer<1, Data,
boost::multi_index::indexed_by,
boost::multi_index::hashed_unique, mpl_::na, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na>, std::allocator<Data> >, TagList = boost::mpl::vector0,
Category = boost::multi_index::detail::ordered_unique_tag]’
mIndex.cc:94: instantiated from here
mIndex.cc:79: error: ‘struct hash_value’ is not a function,
/usr/include/boost/functional/hash/hash.hpp:290: error: conflict with
‘template size_t boost::hash_value(const
std::multimap&)’
/usr/include/boost/functional/hash/hash.hpp:487: error: in call to
‘hash_value’
/usr/include/boost/functional/hash/hash.hpp:487: error: no matching function
for call to ‘hash_value(const Key&)’
/usr/include/boost/functional/hash/hash.hpp:105: note: candidates are:
size_t boost::hash_value(int)
/usr/include/boost/functional/hash/hash.hpp:110: note:
size_t boost::hash_value(unsigned int)
/usr/include/boost/functional/hash/hash.hpp:115: note:
size_t boost::hash_value(long int)
/usr/include/boost/functional/hash/hash.hpp:120: note:
size_t boost::hash_value(long unsigned int)
/usr/include/boost/functional/hash/hash.hpp:229: note:
size_t boost::hash_value(float)
/usr/include/boost/functional/hash/hash.hpp:234: note:
size_t boost::hash_value(double)
/usr/include/boost/functional/hash/hash.hpp:239: note:
size_t boost::hash_value(long double)
make: *** [mIndex.o] Error 1