gpt4 book ai didi

c++ - <未解析的重载函数类型> 调用二元谓词

转载 作者:行者123 更新时间:2023-11-28 04:19:49 25 4
gpt4 key购买 nike

#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 >)'

最佳答案

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

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

LIVE

关于c++ - <未解析的重载函数类型> 调用二元谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55712831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com