an_tang
an_tang
2017-10-11 07:53

数据结构和算法分析 c++描述 模拟List出现的错误

  • c++
  • 数据结构

iterator和const_iterator以及节点Node是类List中嵌套类
iterator继承来自const_iterator
const_iterator里面有Node* current变量以及retrieve()函数
重点来了!!!!!!!!!!!!!!!!!!!
iterator好像看不到上述的那个变量以及函数
编译器一直报错
希望高手帮忙指点一下(限于篇幅已经把一些不相关的函数已经删除)
#include
using namespace std;
template
class List{
private:
struct Node
{
Object data;
Node *prev;
Node *next;
Node(const Object&d=Object(),Node *p=NULL,Node *n=NULL)
:data(d),prev(p),next(n){}

};
public:
class const_iterator
{
public:
const_iterator():current(NULL)
{}
protected://
Node *current;

            Object &retrieve()const
            {
                return current->data;
            }              
            const_iterator(Node *p):current(p)
            {}       
            friend class List<Object>;
    };
    class iterator:public const_iterator
    {
        public:
            iterator()
            {}

            Object & operator*()
            {
                return retrieve();//
                /*
                List.cpp:64:37: error: there are no arguments to ‘retrieve’ that depend on a template parameter, so a declaration of ‘retrieve’ must be available [-fpermissive]
                 return retrieve();
                */
            }
            const Object& operator*()const
            {
                return const_iterator::operator*();
            }
            iterator &operator++()
            {
                current = current->next; ////////   看不到current
                return *this;
                /*List.cpp:64:37: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)

List.cpp: In member function ‘List::iterator& List::iterator::operator++()’:
List.cpp:72:21: error: ‘current’ was not declared in this scope
/
}
iterator operator++(int)
{
iterator old = *this;
++(*this);
return old;
}
protected:
iterator(Node
p):const_iterator(p)
{}

            friend class List<Object>;
    };
    public:

private:
    int theSize;
    Node *head;
    Node *tail;

};
int main()
{
return 0;
}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐

换一换