所以我试图增加一个功能,比较两个std::string
的无视情况从这里:Case insensitive standard string comparison in C++。错误当调用std ::等于一个类成员函数作为比较器
#include <iostream>
#include <string>
using namespace std;
class foo {
public:
bool icompare_pred(char a, char b) {
return std::tolower(a) == std::tolower(b);
}
bool icompare(string a, string b){
return equal(a.begin(), a.end(), b.begin(), icompare_pred);
}
};
int main() {
}
的代码无法编译,甚至更多:它把我带进STL深处有错误
[Error] must use '.*' or '->*' to call pointer-to-member function in '__binary_pred (...)', e.g. '(... ->* __binary_pred) (...)'
这部分
stl_algobase.h
template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
inline bool
equal(_IIter1 __first1, _IIter1 __last1,
_IIter2 __first2, _BinaryPredicate __binary_pred)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
__glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
__glibcxx_requires_valid_range(__first1, __last1);
for (; __first1 != __last1; ++__first1, ++__first2)
if (!bool(__binary_pred(*__first1, *__first2)))
return false;
return true;
}
那么,我该怎么做?我如何解决这个错误?
确保包含所有必需的标题。你缺少算法,可能还有其他的。 – juanchopanza
这是一个基本事实,即成员函数不能通过与非成员函数相同类型的函数指针来使用。 (它有它自己的,但是它们很明显,并且不能隐式转换,原因很明显。) –