2012-03-26 95 views
0

我有一个非常简化的问题,关于升序多索引中的顺序索引。该代码是如下:匹配函数插入升序多索引的顺序索引

我的类存储在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; 

}; 

和主要功能:

#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; 
} 

汇编,测序<>存在时,产率时一个我不明白的错误。你能帮我解决吗?

错误:

$ 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................... 
+0

你可以尝试:'Ls.insert(Ls.begin(),链接(“123”,“456”));'? – Ylisar 2012-03-26 08:30:04

+0

@Ylisar是正确的。它的工作,thanx – rahman 2012-03-26 08:40:50

回答

3

insert预计一个迭代器作为第一个参数指定要插入容器中的位置。看起来multi_index_container也有push_back,这可能更接近你想要的。

所以插入在序列的开头:

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

或者在它的背面:

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

为什么你不包括两个答案到一个。因为你们两个都解决了问题。谢谢 – rahman 2012-03-26 08:41:43

+0

当然,我可以添加一个片段:) – Ylisar 2012-03-26 08:56:20