【发布时间】:2025-12-23 08:00:06
【问题描述】:
有一个类和事件结构:
struct Foo
{
Foo(): name("nx"), val(9) {}
string name;
int val;
};
class Base
{
public:
Base()
{
/*array.push_back(Foo());
array.push_back(Foo());
array.push_back(Foo()); //fill array Foo
array.push_back(Foo());
array.push_back(Foo());*/
}
void Define(Foo* desktop)
{
cout << desktop->name << " " << desktop->val << "\n";
}
void Oblem()
{
/*for(int i = 0; i < array.size(); ++i)
{
Define(&array[i]);
}*/
for_each(array.begin(), array.end(), mem_fun_ref(&Base::Define));
}
public:
std::vector<Foo> array;
};
如何替换 for_each 算法中被注释掉的循环?
我现在有这些错误:
- 错误:不匹配调用 '(std::mem_fun1_ref_t) (Foo&)'|
- 候选对象是:_Ret std::mem_fun1_ref_t<_ret _tp _arg>::operator()(_Tp&, _Arg) const [with _Ret = void, _Tp = Base, _Arg = Foo*]|
【问题讨论】: