0

I am trying to make some wrapper class for multi_index_container.

Basically I want to export only those operations:

  1. Insert Element
  2. Remove Element(for a given element)
  3. Get Element by key

The following code is defined:

    typedef boost::multi_index::multi_index_container<
        SR, boost::multi_index::indexed_by<
        boost::multi_index::ordered_unique<boost::multi_index::tag<SR::ByIdentity>,boost::multi_index::identity<SR> >
        , boost::multi_index::ordered_unique< boost::multi_index::tag<ByName>,boost::multi_index::const_mem_fun<SR,const std::string&
        , &SR::GetName> >
        , boost::multi_index::ordered_non_unique<boost::multi_index::tag<ByID>,boost::multi_index::const_mem_fun<SR, ID
        , &SR::GetID> >
        >
    > SRs;

where:

SR::ByIdentity internal empty struct

ByName and ByID are pre defined empty structs.

those are functions defined in SR

ID SetID(ID ID);
ID GetID() const;

void SetName(const std::string& name);
const std::string& GetName() const;

and this is the code for Insert, Remove and Get

    bool Insert(ElementType val)
    {
        return m_container.insert(val).second;
    }

    void Remove(ElementType val)
    {
        iterator It = m_container.get<ByIdenetity>().find(val);
        if (It != m_container.end())
        {
            m_container.erase(It);
        }
    }

    template<typename Iterator> const_iterator Get(typename Iterator::mtype val) const
    {
        typename Iterator::iterator::const_iterator It = m_container.get<typename Iterator::index>().find(val);
        return It;
    }

where typename Iterator is defined as one of

    struct IdentityIterator
    {
        typedef ByIdentity index;
        typedef SR mtype;
        typedef SRs::index<ByIdentity>::type iterator;
    } ;


    struct NameIterator
    {
        typedef ByName index;
        typedef const std::string& mtype;
        typedef SRs::index<ByName>::type iterator;
    } ;


    struct NameIterator
    {
        typedef ByID index;
        typedef ID mtype;
        typedef SRs::index<ByID>::type iterator;
    } ;

I get The following compile error:

        In member function ?€˜typename ContainerType::const_iterator ns1::ContainerWrapper<ContainerType>::get(typename Iterator::mtype) const [with Iterator = ns2::NameIter, ContainerType = boost::multi_index::multi_index_container<ns2::SR, boost::multi_index::indexed_by<boost::multi_index::ordered_non_unique<boost::multi_index::tag<ns2::ByIdentity, 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>, boost::multi_index::identity<ns2::SR>, mpl_::na>, boost::multi_index::ordered_unique<boost::multi_index::tag<ns2::ByName, 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>, boost::multi_index::const_mem_fun<ns2::SR, const std::string&, &ns2::SR::GetName>, mpl_::na>, boost::multi_index::ordered_non_unique<boost::multi_index::tag<ns2::ByID, 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>, boost::multi_index::const_mem_fun<ns2::SR, int, &ns2::SR::GetRoleID>, 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<ns2::SR> >]?€™:
ns3/testFile.cpp:509:   instantiated from here
../ns1/ContainerWrapper.h:114: error: conversion from ?€˜boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<ns2::SR, std::allocator<ns2::SR> > > > >?€™ to non-scalar type ?€˜boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<ns2::SR, std::allocator<ns2::SR> > > > > >?€™ requested

This does not happen when I remove the boost::multi_index::ordered_unique<boost::multi_index::tag<SR::ByIdentity>,boost::multi_index::identity<SR> >

any Idea?

4

1 回答 1

0

您的Get成员函数const_iterator在应该返回时返回一些Iterator::iterator::const_iterator

于 2013-06-21T18:58:01.137 回答