【问题标题】:comparison function in doubly linked list双向链表中的比较函数
【发布时间】:2014-02-21 22:09:18
【问题描述】:

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

提供了一个头文件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

【问题讨论】:

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


【解决方案1】:

你需要:

typename List<T>::iterator

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

【讨论】:

  • 我认为这解决了部分问题。但我仍然遇到其他错误: List.cpp:344:9: error: 在这种情况下 List.cpp:345:60: error: conversion from âcop4530::List<:basic_string>::const_iteratorâ到非标量类型âcop4530::List<:basic_string> >::iteratorâ请求List.cpp:345:108:错误:从âcop4530::List<:basic_string>::const_iteratorâ转换到非标量类型âcop4530::List<:basic_string> >::iteratorâ请求的List.cpp:345:108:错误:无法从âcop4530::List<:basic_string>转换âitr_2â ::iteratorâ 到 âboolâ
猜你喜欢
  • 2018-10-24
  • 1970-01-01
  • 2021-04-01
  • 1970-01-01
  • 2016-06-11
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多