2013-08-25 116 views
0

我需要創建向量的排序數字索引。並通過不同的字段創建一組T *。比較類型錯誤

這dosnt編譯:布爾比較::運算符()(常量valType &,常量valType &)const的:不能讓從 「POI * const的」 類型轉換的 「常量POI &」。

POI具有2個字段標籤(的std :: string),類型(UINT)

template <typename FieldType, typename valType, FieldType valType::*iMember> 
class Comparator 
{ 
public: 
    bool operator()(valType const& iLeft, valType const& iRight) const { 
     return iLeft->*iMember > iRight->*iMember; 
    } 
}; 

template< class ValType, class CompType, typename FieldType > 
class SearchIndex 
{ 
public: 
    SearchIndex() {} 
    void Build(std::vector<ValType> iElems, std::ofstream & oStream) 
    { 
     std::map< ValType *, size_t > numbersOfElems; 

     for(std::vector<ValType>::iterator it = iElems.begin(); it != iElems.end(); ++it){ 
      m_elems.insert(&(*it)); 
      numbersOfElems.insert(std::pair< ValType * , size_t>(&(*it),5)); 
     } 

     oStream << m_elems.size(); 
     for(std::multiset< ValType * >::iterator it = m_elems.begin(); it!= m_elems.end(); ++it) 
      oStream << numbersOfElems[*it] << " " ; 
    } 

private: 
    std::multiset< ValType * , CompType > m_elems; 
}; 

的main.cpp

int main(int argc, char *argv[]) 
{ 
    std::vector<POI> testVect; 

    POI sec("Gas", 1); 
    testVect.push_back(sec); 
    POI th("Tryy", 3); 
    testVect.push_back(th); 

    std::ofstream oStream; 
    oStream.open("Index.dat",std::ofstream::app | std::ofstream::binary | std::ofstream::out); 

    typedef Comparator<std::string, POI, &POI::m_label> POIbyLabel; 

    SearchIndex< POI, POIbyLabel, std::string> testIndex; 

    testIndex.Build(testVect, oStream); 
} 

回答

0

&(*它)是在SearchIndex鍵入 「POI *」而Comparator的接受參數爲「const POI」(val類型爲const &);嘗試使用m_elems.insert(* it);

+0

這沒有幫助。 std :: multiset m_elems; – YYY

+0

對不起,我錯過了這部分。然後我認爲你需要改變bool operator()(valType const&iLeft,valType const&iRight)的定義,因爲它接受POI參考類型的「valType const&iLeft」,但是你通過聲明「std :: multiset 「是POI指針類型。你可以嘗試將「bool operator()(valType const&iLeft,valType const&iRight)」改爲「bool operator()(valType const * iLeft,valType const * iRight)」。希望這可能會起作用。 @Ubitso – JackyZhu

+0

我可以這麼做。 TY。 – YYY