2013-05-05 98 views
1

我有一个声明如下比较类:模板比较迭代器

template <typename _Type, typename _Comparator = std::equal_to<typename _Type::value_type> > 
     class CSearch { 
public:  
    CSearch() : comp(_Comparator()) { 
    }; 

    CSearch(const _Comparator &_cmp) : comp(_cmp) { 
    }; 
    void Add(int id, const _Type & needle); 
    set<int> Search(const _Type & hayHeap) const;  
protected: 
    _Comparator comp; 
    vector<pair<int, _Type> > m_Db; 
}; 

template <typename _Type, typename _Comparator> 
void CSearch<_Type, _Comparator>::Add(int id, const _Type& needle) { 
    m_Db.push_back(pair<int, _Type>(id, needle)); 
} 

template <typename _Type, typename _Comparator> 
set<int> CSearch<_Type, _Comparator>::Search(const _Type & hayHeap) const { 
    set<int> s_search; 
    typename vector< pair<int, _Type> >::const_iterator it; 
    typename _Type::const_iterator hayStackIterator; 

    for (hayStackIterator = hayHeap.begin(); hayStackIterator != hayHeap.end(); ++hayStackIterator){   
     for(it=m_Db.begin(); it!=m_Db.end(); ++it){ 
      if(comp(*it, *hayStackIterator)) 
       s_search.insert(it->first);     
     } 
    } 
    return s_search; 
} 

//------------------- 
int main(int argc, char** argv) { 

    CSearch <string> test1; 
    test1 . Add (0, "hello"); 
    test1 . Add (1, "world"); 
    test1 . Add (2, "rld"); 
    test1 . Add (3, "ell"); 
    test1 . Add (4, "hell"); 
    printSet (test1 . Search ("hello world!")); 
    // 0, 1, 2, 3, 4 
    printSet (test1 . Search ("hEllo world!")); 
    // 1, 2 

    CSearch <string, bool (*)(const char &, const char &)> test2 (upperCaseCompare); 
    test2 . Add (0, "hello"); 
    test2 . Add (1, "world"); 
    test2 . Add (2, "rld"); 
    test2 . Add (3, "ell"); 
    test2 . Add (4, "hell"); 
    printSet (test2 . Search ("HeLlO WoRlD!")); 
    // 0, 1, 2, 3, 4 
    printSet (test2 . Search ("hallo world!")); 
    // 1, 2 

    CSearch <string, CharComparator> test3 (CharComparator (false)); 
    test3 . Add (0, "hello"); 
    test3 . Add (1, "world"); 
    test3 . Add (2, "rld"); 
    test3 . Add (3, "ell"); 
    test3 . Add (4, "hell"); 
    printSet (test3 . Search ("heLLo world!")); 
    // 0, 1, 2, 3, 4 
    printSet (test3 . Search ("Well, templates are hell")); 
    return 0; 

} 

搜索功能并没有转化条件:

if(comp(*it, *hayStackIterator))... 

你能帮忙吗?

翻译错误:

main.cpp:101:44: instantiated from here main.cpp:55:13: error: no match for call to ‘(const std::equal_to) (const std::pair >&, const char&)’ /usr/include/c++/4.6/bits/stl_function.h:205:12: note: candidate is: /usr/include/c++/4.6/bits/stl_function.h:208:7: note: bool std::equal_to<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = char] /usr/include/c++/4.6/bits/stl_function.h:208:7: note: no known conversion for argument 1 from ‘const std::pair >’ to ‘const char&’ main.cpp: In member function ‘std::set CSearch<_Type, _Comparator>::Search(const _Type&) const [with _Type = std::basic_string, _Comparator = bool (*)(const char&, const char&)]’: main.cpp:112:44: instantiated from here main.cpp:55:13: error: invalid initialization of reference of type ‘const char&’ from expression of type ‘const std::pair >’ main.cpp: In member function ‘std::set CSearch<_Type, _Comparator>::Search(const _Type&) const [with _Type = std::basic_string, _Comparator = CharComparator]’: main.cpp:123:44: instantiated from here main.cpp:55:13: error: no match for call to ‘(const CharComparator) (const std::pair >&, const char&)’ main.cpp:64:7: note: candidate is: main.cpp:69:16: note: bool CharComparator::operator()(const char&, const char&) const main.cpp:69:16: note: no known conversion for argument 1 from ‘const std::pair >’ to ‘const char&’ main.cpp: In member function ‘std::set CSearch<_Type, _Comparator>::Search(const _Type&) const [with _Type = std::vector, _Comparator = std::equal_to]’: main.cpp:134:52: instantiated from here main.cpp:55:13: error: no match for call to ‘(const std::equal_to) (const std::pair >&, const int&)’ /usr/include/c++/4.6/bits/stl_function.h:205:12: note: candidate is: /usr/include/c++/4.6/bits/stl_function.h:208:7: note: bool std::equal_to<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = int] /usr/include/c++/4.6/bits/stl_function.h:208:7: note: no known conversion for argument 1 from ‘const std::pair >’ to ‘const int&’ main.cpp: In member function ‘std::set CSearch<_Type, _Comparator>::Search(const _Type&) const [with _Type = std::vector >, _Comparator = std::equal_to >]’: main.cpp:145:53:
instantiated from here main.cpp:55:13: error: no match for call to ‘(const std::equal_to >) (const std::pair > >&, const std::basic_string&)’ /usr/include/c++/4.6/bits/stl_function.h:205:12: note: candidate is: /usr/include/c++/4.6/bits/stl_function.h:208:7: note: bool std::equal_to<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::basic_string] /usr/include/c++/4.6/bits/stl_function.h:208:7: note: no known conversion for argument 1 from ‘const std::pair > >’ to ‘const std::basic_string&’

+1

难道你不能将你的代码缩减到与问题相关的部分吗? – 0x499602D2 2013-05-05 15:34:59

+0

您的代码不可编译。未找到'printSet','upperCaseCompare','CharComparator'。 – gongzhitaao 2013-05-05 15:37:07

回答

1

错误消息是相当清楚的。

if(comp(*it, *hayStackIterator)) 

*it的类型为const std::pair<int, std::string>&,但*hayStackIterator具有类型const char&。你无法比较它们。