gpt4 book ai didi

c++ - mem_fun_ref : unresolved overloaded function type

转载 作者:太空狗 更新时间:2023-10-29 21:21:52 24 4
gpt4 key购买 nike

以下代码将无法编译,因为“错误:没有匹配函数调用‘mem_fun_ref()’”(gcc 版本 4.4.6)。

#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
#include <iostream>

using namespace std;

class toto
{
char v[10];

public:
toto(char* t) { memcpy(v, t, 9); }
bool test(const char* var) const { return !strncmp(var, v, 9); }
bool test(const string& var) const { return test(var.c_str()); }
};

int main()
{
vector<toto> t;
t.push_back("1");
t.push_back("2");

string name("2");
vector<toto>::iterator it = remove_if(t.begin(), t.end(),
bind2nd(mem_fun_ref(&toto::test), name)); // <= error
t.erase(it, t.end());
return 0;
}

我找到了一个解决方法:创建一个

bool testZ(const string& var) const { return testZ(var); }

但我似乎无法找到正确的模板参数,即使可能的话,也无法将其提供给 mem_fun_ref(或 bind2nd?)以使其在没有我的解决方法的情况下进行编译。

如果没有我的变通办法,是否可以实现此目的,或者变通办法是“首选”方法吗?

最佳答案

你应该可以根据C++ overloaded method pointer来转换它:

bind2nd(mem_fun_ref((bool (toto::*)(const string&) const) &toto::test), name));

关于c++ - mem_fun_ref : unresolved overloaded function type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21709759/

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