比较器类型错误

时间:2013-08-25 14:44:43

标签: c++ templates compare

我需要创建有序矢量索引的索引。并通过不同的字段比较创建一组T *。

这个dosnt编译:bool Comparator :: operator()(const valType&,const valType&)const:无法在“const POI&”中从“POI * const”进行类型转换。

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

1 个答案:

答案 0 :(得分:0)

&amp;(* it)在SearchIndex中是“POI *”类型,而Comparator的接受参数是“const POI”(valType const&amp;);尝试使用m_elems.insert(* it);