[英]error: no match for 'operator=' for operand types std::vector::iterator and __gnu_cxx::__normal_iterator
I'm getting a very nasty error because apparently my iterator types don't match when I try to assign from find_if to a predefined iterator.我收到一个非常严重的错误,因为当我尝试从 find_if 分配给预定义的迭代器时,显然我的迭代器类型不匹配。 I'm not using auto because I want to exclude definitions from the for-loop to avoid reallocation.
我没有使用 auto 是因为我想从 for 循环中排除定义以避免重新分配。
This is the error in essence:这是本质上的错误:
../components/aurora_fsm/aurora_fsm.cpp: In member function 'FSM_StateBase* FSM_StateBase::FollowBits(EventBits_t)':
../components/aurora_fsm/aurora_fsm.cpp:43:118: error: no match for 'operator=' (operand types are 'std::vector<FSM_StateBase*>::iterator' {aka '__gnu_cxx::__normal_iterator<FSM_StateBase**, std::vector<FSM_StateBase*> >'} and '__gnu_cxx::__normal_iterator<FSM_StateBase* const*, std::vector<FSM_StateBase*> >')
[&s_checked](FSM_StateBase* const state) { if (!s_checked.count(state)) { return true; } return false; } );
And this is my implementation of the method in question with just the relevant details:这是我对相关方法的实现,仅包含相关细节:
FSM_StateBase* FSM_StateBase::FollowBits()
{
std::vector<FSM_StateBase*> v_tmp_path = { this };
std::unordered_set<FSM_StateBase*> s_checked;
std::vector<FSM_StateBase*>::iterator it;
for (;;) {
// find new state that is not checked
auto& v_adjacent = v_tmp_path.back()->GetAdjacentStates();
it = std::find_if(begin(v_adjacent), end(v_adjacent),
[&s_checked](FSM_StateBase* const state) { if (!s_checked.count(state)) { return true; } return false; } );
// ...
}
// ..
}
Yet I have no idea what to change to make this work?但我不知道要改变什么才能使这项工作? After all, find_if is supposed to return an iterator for a vector of the specified type (
FSM_StateBase*
in my case) which is exactly what I'm defining it
to be..毕竟, find_if 应该为指定类型的向量(在我的例子中为
FSM_StateBase*
)返回一个迭代器,这正是我定义it
......
EDIT (for clarity):编辑(为清楚起见):
const std::vector<FSM_StateBase*>& FSM_StateBase::GetAdjacentStates(void) {
return adjacent_states_;
}
with和
class FSM_StateBase
{
protected:
std::vector<FSM_StateBase*> adjacent_states_;
// ...
}
std::find_if
will return the same type of iterator that was passed to it. std::find_if
将返回传递给它的相同类型的迭代器。
v_adjacent
will have the type const std::vector<FSM_StateBase*>&
. v_adjacent
的类型为const std::vector<FSM_StateBase*>&
。 Notice the const
being part of the type.注意
const
是类型的一部分。 That const
means the iterators for this container will be constant iterators.这个
const
意味着这个容器的迭代器将是常量迭代器。
it
, on the other hand, is not a constant iterator. it
,另一方面,是不是一个常数迭代器。
There's a hint in the error message where it says that the left-hand side of the assignment (the variable it
) is the type错误消息中有一个提示,它说赋值的左侧(变量
it
)是类型
__gnu_cxx::__normal_iterator<FSM_StateBase**, std::vector<FSM_StateBase*> >
And the type of the right-hand side of the assignment (what is returned by std::find_if
) is赋值右侧的类型(
std::find_if
返回的std::find_if
)是
__gnu_cxx::__normal_iterator<FSM_StateBase* const*, std::vector<FSM_StateBase*> >
Notice the const
in FSM_StateBase* const*
.注意
const
在FSM_StateBase* const*
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.