0

我需要创建向量的排序数的索引。并创建一组由不同字段比较的 T *。

这个 dosnt 编译: bool Comparator::operator ()(const valType &,const valType &) const: Cant make type cast from "POI *const" in "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;
};

主文件

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

1 回答 1

0

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

于 2013-08-25T14:55:46.093 回答