我在双向链接列表中执行比较功能时遇到了一些麻烦,目的是“检查两个列表是否包含相同的元素序列。两个列表相等,如果它们具有相同数量的元素并且在相应位置具有相同的元素相等”,这似乎是正确的,但是在尝试编译时出现错误。这是代码,与一个单独的头文件一起使用,其描述是:
提供了一个头文件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
你需要:
typename List<T>::iterator
因为迭代器是一个从属名称。(名称的种类取决于模板参数)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句