foreach+成员函数指针问题-》高手进
// 成员函数调用申明
class CTaskLogic
{
public:
void FillFinishFlag(tagTaskFinish& tTagTaskFinish) const;
private:
struct tagTaskFinish
{
tagTaskFinish(int ntTaskID, TASKSTATE tenmTaskState):
nTaskID(ntTaskID),enmTaskState(tenmTaskState){}
int nTaskID;
TASKSTATE enmTaskState;
};
}
// 调用
vector<tagTaskFinish> tNPCTaskVec;
for_each(tNPCTaskVec.begin(), tNPCTaskVec.end(), ind1st(mem_fun_ref(&CTaskLogic::FillFinishFlag),*this));
// 错误提示
DescriptionResourcePathLocationType
错误:‘typename _Operation::result_type std::binder1st<_Operation>::operator()(typename _Operation::second_argument_type&) const [with _Operation = std::const_mem_fun1_ref_t<void, CTaskLogic, CTaskLogic::tagTaskFinish&>]’无法被重载TaskLogicline 117, external location: /usr/include/c++/4.4.4/backward/binders.hC/C++ Problem
DescriptionResourcePathLocationType
错误:与‘typename _Operation::result_type std::binder1st<_Operation>::operator()(const typename _Operation::second_argument_type&) const [with _Operation = std::const_mem_fun1_ref_t<void, CTaskLogic, CTaskLogic::tagTaskFinish&>]’TaskLogicline 111, external location: /usr/include/c++/4.4.4/backward/binders.hC/C++ Problem
[解决办法]
void FillFinishFlag(const tagTaskFinish& tTagTaskFinish) const;
这样试试。
反正是const的问题。
[解决办法]
1.CTaskLogic 的对象 能转成 tagTaskFinish 吗?
2.CTaskLogic::FillFinishFlag这个函数就一个参数,你要binder1st干什么?
[解决办法]
for_each(tNPCTaskVec.begin(), tNPCTaskVec.end(), mem_fun_ref(&CTaskLogic::FillFinishFlag));
[解决办法]
void FillFinishFlag(tagTaskFinish& tTagTaskFinish) const;
[解决办法]
标准适配器无法满足LZ的需求。LZ可以自己写一个functor。
class MemberCaller : std::unary_function<const tagTaskFinish *, void>{public: explicit MemberCaller(CTaskLogic *taskLogic) : taskLogic_(taskLogic) { assert(NULL != taskLogic); } result_type operator()(argument_type elem) { taskLogic_->FillFinishFlag(elem); }private: CTaskLogic *taskLogic_;};for_each(tNPCTaskVec.begin(), tNPCTaskVec.end(), MemberCaller(this));
[解决办法]
或者LZ可以尝试一下TR1的bind。
[解决办法]