简体   繁体   English

错误:对于操作数类型 std::vector::iterator 和 __gnu_cxx::__normal_iterator 不匹配“operator=”

[英]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* .注意constFSM_StateBase* const*

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法转换 '__gnu_cxx::__normal_iterator <player*, std::vector<player> &gt;' 到 'int' 作为回报</player*,> - error: cannot convert ‘__gnu_cxx::__normal_iterator<Player*, std::vector<Player> >’ to ‘int’ in return 不匹配 &#39;operator=&#39;(操作数类型是 &#39;__gnu_cxx::__alloc_traits <std::allocator<std::vector<int> &gt; &gt; - no match for 'operator=' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > > 在C ++中,得到一个错误说“std :: string {aka std :: basic_string <char> }&#39;不是从&#39;const __gnu_cxx :: __ normal_iterator &lt;_IteratorL,_Container&gt;派生的 - in C++, get an error says “std::string {aka std::basic_string<char>}' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>” 错误:要求在“ it .__ gnu_cxx :: __ normal_iterator &lt;_Iterator,_Container&gt; :: operator *中要求成员“发言” * - error: request for member ‘speak’ in ‘it.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* 我遇到了不转换 const__gnu_cxx::__normal_iterator 的问题<long long int* , std:: vector<long long int> - I'm having an issue with not converting const__gnu_cxx::__normal_iterator<long long int* , std:: vector<long long int> 错误:“ operator =”不匹配(操作数类型为“ std :: map” <int, double> ::迭代器 - error: no match for 'operator=' (operand types are 'std::map<int, double>::iterator 错误&#39;没有匹配呼叫向量 <int> normal_iterator <int*, vector<int> &gt; :: difference_type)” - Error 'no match for call to vector<int> normal_iterator<int*, vector<int> >::difference_type)' 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream' {aka 'std::basic_ostream<char> '} 和 'std::_List_iterator<int> ')</int></char> - error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘std::_List_iterator<int>’) 错误:'operator+' 不匹配(操作数类型是 'std::vector<int> ' 和 'int')</int> - error: no match for ‘operator+’ (operand types are ‘std::vector<int>’ and ‘int’) 错误:'operator+' 不匹配(操作数类型为 'std::__cxx11::list<int> ' 和 'int')|</int> - Error: no match for 'operator+' (operand types are 'std::__cxx11::list<int>' and 'int')|
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM