Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++/g++;重载增量运算符_C++_G++_Overloading_Increment - Fatal编程技术网

C++ C++/g++;重载增量运算符

C++ C++/g++;重载增量运算符,c++,g++,overloading,increment,C++,G++,Overloading,Increment,我正在尝试实现一个双链接列表,并希望创建一个迭代器。结构如下: template<class type> class List { size_t listElementCnt; ... public: ... class iterator { ... public: ... iterator& operator ++(); iterator operator ++(int)

我正在尝试实现一个双链接列表,并希望创建一个迭代器。结构如下:

template<class type>
class List {
    size_t listElementCnt;
    ...
public:
    ...
    class iterator {
        ...
    public:
        ...
        iterator& operator ++();
        iterator operator ++(int);
        ...
    };
    ...
 };
模板
班级名单{
列表项的大小;
...
公众:
...
类迭代器{
...
公众:
...
迭代器和运算符++();
迭代器运算符++(int);
...
};
...
};
现在我想实现重载两个操作符:

template<class type>
typename iterator& List<type>::iterator::operator ++() {
    ...
}
template<class type>
typename iterator List<type>::iterator::operator ++(int) {
    ...
}
模板
类型名迭代器&列表::迭代器::运算符++(){
...
}
模板
typename迭代器列表::迭代器::运算符++(int){
...
}
现在有两个错误:

  • 找不到成员声明
  • 无法解析类型“迭代器”

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

< p>您需要在返回类型中限定<代码>迭代器< /> >:

template<class type>
typename List<type>::iterator& List<type>::iterator::operator ++() {
    ...
}
template<class type>
typename List<type>::iterator List<type>::iterator::operator ++(int) {
    ...
}
模板
类型名列表::迭代器&列表::迭代器::运算符++(){
...
}
模板
类型名称列表::迭代器列表::迭代器::运算符++(int){
...
}

在成员函数的越界定义中,函数的返回类型不在类范围内,因为尚未看到类名。因此,请将定义更改为如下所示:

template<class type>
typename List<type>::iterator& List<type>::iterator::operator ++() {
    ...
}
template<class type>
typename List<type>::iterator List<type>::iterator::operator ++(int) {
    ...
}
模板
类型名列表::迭代器&列表::迭代器::运算符++(){
...
}
模板
类型名称列表::迭代器列表::迭代器::运算符++(int){
...
}

谢谢。我花了很多时间来解决这个问题,现在明白了,我犯了多么微不足道的错误。。。