情况有点复杂,但我会尽量解释清楚。
我正在尝试从对象指针向量中获取指向对象的指针,其中对象的属性之一与变量字符串匹配。为此,我将字符串变量绑定到一个函数,然后使用绑定函数尝试找到该对象指针。但是,每当我尝试此操作时,它都会崩溃。我已经尝试了很多不同的测试来找到问题所在,但我仍然一无所知。相关代码如下:
class A {
std::string n;
...
public:
const std::string getN() {
return n
}
};
static bool checkN(std::string n1,A* a) {
if (a->getN() == n1) {
return true;
}
else {
return false;
}
}
void function() {
using namespace std::placeholders;
A* a;
std::string compare = "Yes";
const std::vector<A*> As; //As[0].n = "Yes";
auto check = std::bind(checkN,compare,_1);
a = (*std::find_if(As.begin(),As.end() - 1,check)); //This is the line where it crashes.
}
请注意,这是一个简化版本,但我认为它可以理解。有什么建议么? 编辑:在尝试简化代码时犯了一些语法错误。修复了它们。