2010-12-15 81 views
3

所以,我試圖涉及multi_index_container,我有一個相當奇怪的編譯器錯誤,首先這裏是最簡單的例子來演示我的問題(我可能會丟失東西太簡單)...Boost multi_index_container,通過標記獲得索引導致編譯器錯誤

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

namespace multi_index = boost::multi_index; 

template <typename _IdType> 
class A 
{ 
public: 
    typedef _IdType IdType; 
    IdType getId() const { return id; } 

private: 
    IdType id; 
}; 

struct id_index{}; 

template <typename Traits> 
class Container 
{ 
    typedef typename Traits::AType AType; 
    typedef typename AType::IdType IdType; 

    typedef typename multi_index::multi_index_container< 
    AType, 
    multi_index::indexed_by< 
     // sort by Id 
     multi_index::ordered_non_unique<multi_index::tag<id_index>, BOOST_MULTI_INDEX_CONST_MEM_FUN(AType, IdType, getId) > 
    > 
    > ASet; 

    typedef typename ASet::template index<id_index>::type::const_iterator a_it; 
    typedef typename ASet::template index<id_index>::type::reverse_iterator a_rit; 

    typedef typename ASet::template index<id_index>::type id_index_t; 

public: 

    bool addA(AType const& cA) 
    { 
    const id_index_t& os = _cSet.get<id_index>(); // line 1: errors occur here 
    // .. do stuff 
    return true; 
    } 

private: 
    // Instance of the container... 
    ASet _cSet; 
}; 

struct ATraits 
{ 
    typedef A<int> AType; 
}; 

int main(void) 
{ 
    Container<ATraits> container; 

    ATraits::AType a; 

    container.addA(a); 

    return 0; 
} 

由G報告的錯誤++(GCC 4.4.4,Linux)的是:

error: expected primary-expression before ‘>’ token (@ line 1) 
error: expected primary-expression before ‘)’ token (@ line 1) 

因此,這是工作的罰款,直到我轉換的容器類模板,在此之後,我得到這個錯誤,並不能解決爲什麼..

任何想法可以理解的......

+0

我真的不知道這是爲什麼發生,但VC2010編譯代碼罰款! – AraK 2010-12-15 15:37:34

+0

@AraK,我想這是一個gcc odity,但@skwllsp的解決方案現在工作正常! – Nim 2010-12-15 15:48:17

回答

6
bool addA(AType const& cA) 
    { 
    const id_index_t& os = _cSet.template get<id_index>(); // line 1: errors occur here 
    // .. do stuff 
    return true; 
    } 
+0

ARRGH ...我認爲這可能很簡單!這是模板/ typename地獄...我真的放棄了,並使用'get (_cSet)',但我想我會回到這個。謝謝! – Nim 2010-12-15 15:46:58

+0

@Nim:不要,免費功能是要走的路:) – 2010-12-15 17:53:02

+0

@Metthieu,真的嗎?但是,成員函數是否可以利用免費函數可能無法實現的一些實現細節呢? – Nim 2010-12-15 20:59:49