c++boostboost-multi-index

matching function for insert in sequenced indices of boost multi index


I have a very simplified question regarding sequenced indices in boost multi index. The code is as below:

my class stored in Link.hpp:

#include <string>

class Link  {
public:
    Link(std::string l,std::string r) :linkID(l),roadName(r) {}
    Link() {}
    std::string roadName;
    std::string linkID;

};

and the main function:

#include "Link.hpp"
#include <string>
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/key_extractors.hpp>

using boost::multi_index::multi_index_container;
using boost::multi_index::ordered_non_unique;
using boost::multi_index::ordered_unique;
using boost::multi_index::sequenced;
using boost::multi_index::indexed_by;
using boost::multi_index::member;

typedef boost::multi_index::multi_index_container<Link,
        indexed_by<sequenced<> > > Links;

int main() {
    Links Ls;
    Ls.insert(Link("123", "456"));

    return 1;
}

the compilation, when sequenced<> is present, yields an error I don't understand. could you please help me with that?

Error:

$ g++ Links.cpp 
Links.cpp: In function ‘int main()’:
Links.cpp:29:29: error: no matching function for call to ‘boost::multi_index::multi_index_container<Link, boost::multi_index::indexed_by<boost::multi_index::sequenced<> > >::insert(Link)’
Links.cpp:29:29: note: candidates are:
/usr/include/boost/multi_index/sequenced_index.hpp:267:28: note: std::pair<boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::sequenced_index_node<typename SuperMeta::type::node_type> >, bool> boost::multi_index::detail::sequenced_index<SuperMeta, TagList>::insert(boost::multi_index::detail::sequenced_index<SuperMeta, TagList>::iterator, boost::multi_index::detail::sequenced_index<SuperMeta, TagList>::value_param_type) [with SuperMeta = boost::multi_index::detail::nth_layer<1, Link, boost::multi_index::indexed_by<boost::multi_index::sequenced<> >, std::allocator<Link> >, TagList = boost::mpl::vector0<mpl_::na>, typename SuperMeta::type::node_type = boost::multi_index::detail::index_node_base<Link, std::allocator<Link> >, boost::multi_index::detail::sequenced_index<SuperMeta, TagList>::iterator = boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::sequenced_index_node<boost::multi_index::detail::index_node_base<Link, std::allocator<Link> > > >, boost::multi_index::detail::sequenced_index<SuperMeta, TagList>::value_param_type = const Link&]
/usr/include/boost/multi_index/sequenced_index.hpp:267:28: note:   candidate expects 2 arguments, 1 provided...................

Solution

  • insert expects an iterator as first argument to specify where within the container you want to insert. It would seem multi_index_container also have push_back, which probably is closer to what you want.

    So to insert at the beginning of the sequence:

    Ls.insert( Ls.begin(), Link( "123", "456" ) );
    

    Or at the back of it:

    Ls.push_back( Link( "123", "456" ) );