2012-03-26 59 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