2013-03-23 76 views
2

我想实现一个双链表,并且想要创建一个迭代器。其结构是:C++/g ++过载增量运算符

template<class type> 
class List { 
    size_t listElementCnt; 
    ... 
public: 
    ... 
    class iterator { 
     ... 
    public: 
     ... 
     iterator& operator ++(); 
     iterator operator ++(int); 
     ... 
    }; 
    ... 
}; 

现在我要实现的过载无论是运营商:

template<class type> 
typename iterator& List<type>::iterator::operator ++() { 
    ... 
} 
template<class type> 
typename iterator List<type>::iterator::operator ++(int) { 
    ... 
} 

现在有两个误区:

  • 成员声明没有找到
  • 类型“迭代“无法解决

当我重载其他运算符(如解引用或( - )等于运算符)时,没有错误。错误只出现在g ++ - 编译器中。 visual C++的编译器不会显示任何错误,它在那里工作得很好。

回答

4

在成员函数的乱线定义,函数的返回类型是不上课的范围,因为类名尚未见过。因此,请将您的定义更改为如下所示:

template<class type> 
typename List<type>::iterator& List<type>::iterator::operator ++() { 
    ... 
} 
template<class type> 
typename List<type>::iterator List<type>::iterator::operator ++(int) { 
    ... 
} 
+0

谢谢。这个问题花了很多时间,现在看到,我犯了什么微不足道的错误...... – 2013-03-23 11:50:44

3

需要判定iterator在返回类型:

template<class type> 
typename List<type>::iterator& List<type>::iterator::operator ++() { 
    ... 
} 
template<class type> 
typename List<type>::iterator List<type>::iterator::operator ++(int) { 
    ... 
}