双向链表中的比较函数

时间:2014-02-21 22:09:19

标签: c++ list doubly-linked-list

我在双向链表中做比较函数时遇到了一些麻烦,其目的是“检查两个列表是否包含相同的元素序列。如果两个列表具有相同数量的元素,则两个列表相等相应位置的元素是相同的“看起来是正确的,但是当我尝试编译时,我遇到了错误。这是代码,与单独的头文件一起使用,其描述如下:

  

提供了一个头文件List.h,它包含双向链表类模板List的接口。

这是我的比较功能:

template <typename T>
bool operator==(const List<T> & lhs, const List<T> & rhs){
    if (lhs.theSize == rhs.theSize){
/*line345*/ for(List<T>::iterator itr = lhs.begin(), List<T>::iterator itr_2 = rhs.begin(); itr != lhs.end(); ++itr, ++itr_2){

            if(*itr != *itr_2)
            return false;
        }
        return true;
    }
    else
    return false;
}

如果我需要提供更多代码,请告诉我。我的错误是:

List.cpp:345:35: error: expected ';' before 'itr'
List.cpp:345:93: error: 'itr' was not declared in this scope
List.cpp:345:120: error: 'itr_2' was not declared in this scope
List.cpp:344:9: error: within this context
List.cpp:345:91: error: dependent-name 'cop4530::List<T>::iterator' is parsed as a non-type, but instantiation yields a type

1 个答案:

答案 0 :(得分:0)

你需要:

typename List<T>::iterator

因为迭代器是依赖名称。 (名称的类型取决于模板参数)