当试图让const_iterator查找元素是否存在于集合中时,得到编译错误

Getting compilation error while trying to get const_iterator to find the element exist in the set or not

本文关键字:集合 错误 编译 于集合 const iterator 存在 是否 元素 查找      更新时间:2023-10-16
template <class T>
Node<T>* LinkedList<T>::findCommonNode_hash(LinkedList<T>* list_2)
{
         unordered_set<Node<T>*>* set = new unordered_set<Node<T>*>();
         Node<T>* curr_list_1 = head->next;
         Node<T>* curr_list_2 = list_2->head->next;
         unordered_set<Node<T>*>::const_iterator itr = set->find(curr_list_1);
         if(itr == set->end())
         {
             set->insert(curr_list_1);
         }
         else
            return curr_list_1;
         unordered_set<Node<T>*>::const_iterator itr1 = set->find(curr_list_2);
         if((itr1 == set->end()))
         {
             set->insert(curr_list_2);
         }
         else
            return curr_list_2;
         return nullptr;
}

IDE:代码块c++ 11。当试图获得const_iterator来查找元素是否存在于集合中时,我得到了下面的错误。

 C:EducationDataStructure_AlgorithmsListList.cpp|252|
error: need 'typename' before 'std::unordered_set<Node<T>*>::
const_iterator' because 'std::unordered_set<Node<T>*>' is a dependent scope|

错误信息提示您必须添加typename关键字,因为在这种情况下const_iterator是一个依赖名称。

:

typename unordered_set<Node<T>*>::const_iterator itr = set->find(curr_list_1);

当然,其他类似的行也是如此。

无论如何,你的代码似乎没有任何意义,但我猜那是因为它只是在进行中的工作。