<unresolved重载=“” function =“” type =“”>调用二进制谓词

时间:2019-04-16 16:33:06

标签: c++ templates

#include <iostream>
#include <iterator>
#include <vector>

using namespace std;

template<typename Iter1, typename Iter2>
bool pred(Iter1 it1, Iter2 it2){
    if(*it1 == *it2)
        return true;
    return false;
}

template<typename FwdIter1, typename InIter2, typename BinPred>
bool contains(FwdIter1 first1, FwdIter1 last1, InIter2 first2, InIter2 last2, BinPred pred){
    for(InIter2 it2 = first2; it2 != last2; ++it2){
        for(InIter2 it1 = first1; it1 != last1; ++it1){
            if(pred(it1, it2)){
                return true;
            }
        }
    }
    return false;
}

int main()
{
    vector<int> v1 {2, 5, 6, 8};
    vector<int> v2 {1, 2, 3, 10};
    if (contains(v1.begin(), v1.end(), v2.begin(), v2.end(), pred))
        cout << "Element found" << '\n';
    else
        cout << "Element not found \n";
    return 0;
}

为什么会这样?

error: no matching function for call to 'contains(std::vector<int>::iterator, std::vector<int>::iterator, std::vector<int>::iterator, std::vector<int>::iterator, < unresolved overloaded function type >)'

1 个答案:

答案 0 :(得分:0)

您必须为pred指定模板参数,例如

if (contains(v1.begin(), v1.end(), v2.begin(), v2.end(), 
             pred<decltype(v1.begin()), decltype(v2.begin())>))

LIVE