gpt4 book ai didi

c++ - 使用二进制谓词在 find_first_of 函数上给出错误

转载 作者:行者123 更新时间:2023-11-28 00:44:26 26 4
gpt4 key购买 nike

这是代码

#include <string>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

class test {
struct con {
string s1;
string s2;
};
public:
void somefunc();

private:
bool pred(con c1, con c2);
vector<con> vec;
};

void test::somefunc()
{
vector<con> v;
vector<con>::iterator it = find_first_of(vec.begin(), vec.end(), v.begin(), v.end(), pred);
}

bool test::pred(con c1, con c2)
{
return 0;
}

它给出了错误

est.cpp(24) : error C2664: 'struct test::con *__cdecl std::find_first_of(struct test::con *,struct test::con *,struct test::con *,struct test: :con *,bool (__thiscall *)(结构测试::con,struct test::con))':无法将参数 5 从'bool (struct test::con,struct test::con)'转换为'bool (__thiscall *)(struct test::con,struct test::con) ' 范围内具有此名称的函数均不匹配目标类型

我不明白什么是 (__thiscall*) 以及如何将我的谓词函数转换为它。

最佳答案

您的谓词不能是非静态成员函数,因为它采用隐式第一个参数,总共提供三个参数。您需要静态成员函数或非成员函数:

// non-member function
bool pred(const con& c1, const con& c2)
{
return false;
}

void test::somefunc()
{
vector<con> v;
vector<con>::iterator it = find_first_of(vec.begin(), vec.end(),
v.begin(), v.end(),
pred);
}

或者,使用 std::bindthis 绑定(bind)为第一个参数:

using namespace std::placeholders;
vector<con>::iterator it = find_first_of(vec.begin(), vec.end(),
v.begin(), v.end(),
std::bind(&test::pred, this, _1, _2));

std::bind 的调用生成一个带有两个 con 参数的可调用实体。它在内部存储了 this 指针的拷贝(但 this 未在您的 pred 函数中使用)。

关于c++ - 使用二进制谓词在 find_first_of 函数上给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17048647/

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