我有一个声明如下比较器的类:
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:从这里实例化main.cpp:55:13:错误:没有 匹配调用'(const std :: equal_to)(const std :: pair&gt;&amp;,const char&amp;)' /usr/include/c++/4.6/bits/stl_function.h:205:12:注意:候选人是: /usr/include/c++/4.6/bits/stl_function.h:208:7:注意:bool std :: equal_to&lt; _Tp&gt; :: operator()(const _Tp&amp;,const _Tp&amp;)const [with _Tp = char] /usr/include/c++/4.6/bits/stl_function.h:208:7:注意:参数1从'const std :: pair&gt;'到'const char&amp;'main.cpp没有已知的转换:在会员 function'std :: set CSearch&lt; _Type,_Comparator&gt; :: Search(const _Type&amp;)const [with _Type = std :: basic_string,_Comparator = bool(*)(const char&amp;,const char&amp;)]':main.cpp:112:44:instantiated 从这里main.cpp:55:13:错误:引用初始化无效 'const char&amp;'类型'const std :: pair&gt;'main.cpp类型的表达式:在成员函数'std :: set中 CSearch&lt; _Type,_Comparator&gt; ::搜索(const _Type&amp;)const [with _Type = std :: basic_string,_Comparator = CharComparator]': main.cpp:123:44:从这里实例化main.cpp:55:13:错误:没有 匹配调用'(const charComparator)(const std :: pair&gt;&amp;,const char&amp;)'main.cpp:64:7:注意: 候选人是:main.cpp:69:16:注意:bool CharComparator :: operator()(const char&amp;,const char&amp;)const main.cpp:69:16:注意:来自'const的参数1没有已知的转换 std :: pair&gt;'到'const char&amp;'main.cpp: 在成员函数'std :: set CSearch&lt; _Type中, _Comparator&gt; :: Search(const _Type&amp;)const [with _Type = std :: vector,_Comparator = std :: equal_to]':main.cpp:134:52: 从这里实例化main.cpp:55:13:错误:无法匹配调用 '(const std :: equal_to)(const std :: pair&gt;&amp;, const int&amp;)'/ usr /include/c++/4.6/bits/stl_function.h:205:12:注意: 候选人是:/usr/include/c++/4.6/bits/stl_function.h:208:7:注意: bool std :: equal_to&lt; _Tp&gt; :: operator()(const _Tp&amp;,const _Tp&amp;)const [with _Tp = int] /usr/include/c++/4.6/bits/stl_function.h:208:7:注意: 参数1从'const std :: pair&gt;'到'const int&amp;'main.cpp没有已知的转换:在成员函数中 'std :: set CSearch&lt; _Type,_Comparator&gt; :: Search(const _Type&amp;)const [with _Type = std :: vector&gt;,_ Comparator = std :: equal_to&gt;]':main.cpp:145:53:
从这里实例化main.cpp:55:13:错误:无法匹配调用 '(const std :: equal_to&gt;)(const std :: pair&gt;&gt;&amp;,const 的std :: basic_string的&安培)” /usr/include/c++/4.6/bits/stl_function.h:205:12:注意:候选人是: /usr/include/c++/4.6/bits/stl_function.h:208:7:注意:bool std :: equal_to&lt; _Tp&gt; :: operator()(const _Tp&amp;,const _Tp&amp;)const [with _Tp = std :: basic_string] /usr/include/c++/4.6/bits/stl_function.h:208:7:注意:不知道 从'const std :: pair&gt;转换参数1 &gt;'到'const 的std :: basic_string的&安培;”
答案 0 :(得分:1)
错误信息非常清楚。
if(comp(*it, *hayStackIterator))
*it
的类型为const std::pair<int, std::string>&
,但*hayStackIterator
的类型为const char&
。你无法比较它们。