无效使用非静态数据成员'linkedList< int> :: dummyNode'c ++ [英] invalid use of non-static data member 'linkedList<int>::dummyNode' c++

查看:445
本文介绍了无效使用非静态数据成员'linkedList< int> :: dummyNode'c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在dummyNode声明变量是工作顺利,直到我写迭代器类为嵌套,现在它给我一个错误无效使用非静态数据成员的,链表:: dummyNode C ++,如果我删除iterator类效果很好
模板

The dummyNode declaration variable was working well until i wrote iterator class as nested, now it gives me an error invalid use of non-static data member, 'linkedList::dummyNode' c++, if i removed iterator class it works well template

class linkedList
{

private:
    listNode<T> * head, *tail;
    listNode<T> *  dummyNode = new listNode<T>;
    int sz = 0;
public:
    class iterator
    {
    public:
        iterator()
        {
            itrNode = head;
        }
        void operator ++ ()
        {
            try{
                if(itrNode == dummyNode)
                    throw "Sorry this is the end of the list\n";
                else
                {
                    itrNode = itrNode->next;
                }

            }catch(const char * error)
            {
                cerr << error;
            }
        }
        T& operator *()
        {
            return *(itrNode->value);
        }
        void operator -- ();
    private:
        listNode<T> * itrNode;

    };
    linkedList();
    ~linkedList();
    linkedList(T value, int initial_size);
    iterator begin();


};


推荐答案

一个C ++嵌套类不与它的外部共享数据class - 如果有这种情况,内部类的每个实例将与外部类的相应实例处于一对一的关系中,因此不会添加额外的功能。相反,嵌套类的主要用途是:

A C++ nested class do not share data with its outer class -- if there were the case, each instance of the inner class would be in a one-to-one relationship with a corresponding instance of an outer class and would thus add no extra functionality. Instead, the main purposes of nested classes are:


  1. 命名空间含义: linkedlist :: iterator 大于 linkedlist_iterator

  2. 根据C ++版本标准,嵌套类有实例的成员对象的额外能见度外部阶级

  3. 也许其他我不知道的事情

  1. Namespace meaning: linkedlist::iterator is more meaningful than linkedlist_iterator
  2. Depending on the C++ version standard, a nested class has extra visibility of the member objects of instances of the outer class
  3. Maybe others that I'm not aware of

最大的问题在你的代码中是迭代器的构造函数

The biggest problem in your code is the constructor of the iterator

    iterator()
    {
        itrNode = head;
    }

数据在内部类的上下文中没有任何意义,因为它是 linkedlist 对象的数据成员 - 再次,迭代器属于 linkedlist 类的实例。代替的构造应该是这样的

The data head means nothing in the context of the inner class, because its a data member of a linkedlist object -- again, the iterator does not belong to an instance of the linkedlist class. Instead the constructor should be something like

    iterator(linkedlist<T>& list)
    {
        itrNode = list.head; // Might not work in C++03
    }

您可能还有其他更改需要关于模板化(我不确定;我之前没有制作嵌套的模板化类)。此外,链接列表的这种类型的构造函数应该可以工作,但不遵循标准范例。更标准的是添加函数

There might be other changes you need to make regarding the templatization (I'm not sure; I haven't made nested templated classes before). Also, this type of constructor for the linked list should work, but doesn't follow standard paradigms. More standard would be adding functions

iterator linkedlist::begin();
iterator linkedlist::end();

linkedlist 类。更好的方法是创建 begin(linkedlist&) end(linkedlist&)函数。这在解释了这个以下SO帖子

to the linkedlist class. Even better would be to create begin(linkedlist&) and end(linkedlist&) functions. This is explained in this following SO post.

这篇关于无效使用非静态数据成员'linkedList&lt; int&gt; :: dummyNode'c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆