Question

I have a multi_index iterator defined in my class. I need to give the user the permission to iterate through he container based on a certain index. I just don't know how to do it. could you help me please:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>

using namespace ::boost;
using namespace ::boost::multi_index;

struct RoadNetwork{
        int n;
        int m;
        RoadNetwork(int q = 0,int p = 0){ n = q; m = p;};
 };

class A
{
public:
    typedef multi_index_container<
    RoadNetwork,
        indexed_by<
            ordered_unique<identity<RoadNetwork> >,
            ordered_non_unique<member<RoadNetwork, int, &RoadNetwork::n> >

        >
    > mindex;
    mindex i;

        const mindex::nth_index<0>::type& iterator_begin() const
        {
            return i.get<0>().begin();
        }

};

int main(void){
    A a;

    return 0;
}

the error is:

~/workspace/multiIndex$ g++ main.cpp   main.cpp: In member function ‘const type& A::iterator_begin() const’: main.cpp:30:34: error: invalid initialization of reference of type ‘const type& {aka const boost::multi_index::detail::ordered_index<boost::multi_index::identity<RoadNetwork>, std::less<RoadNetwork>, boost::multi_index::detail::nth_layer<1, RoadNetwork, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::identity<RoadNetwork>
>, 

boost::multi_index::ordered_non_unique<boost::multi_index::member<RoadNetwork, int, &RoadNetwork::n> > >, std::allocator<RoadNetwork> >, boost::mpl::vector0<mpl_::na>, boost::multi_index::detail::ordered_unique_tag>&}’ from expression of type ‘boost::multi_index::detail::ordered_index<boost::multi_index::identity<RoadNetwork>,


std::less<RoadNetwork>, boost::multi_index::detail::nth_layer<1, RoadNetwork, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::identity<RoadNetwork>
>, 

boost::multi_index::ordered_non_unique<boost::multi_index::member<RoadNetwork, int, &RoadNetwork::n> > >, std::allocator<RoadNetwork> >, boost::mpl::vector0<mpl_::na>, boost::multi_index::detail::ordered_unique_tag>::const_iterator {aka 

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<RoadNetwork, std::allocator<RoadNetwork> > > > >}’

I value your kind help.

Thank you and regards

Was it helpful?

Solution

iterator_begin() function return type is incorrect. Correct declaration is:

mindex::nth_index_iterator<0>::type iterator_begin() const;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top