无效使用非静态数据成员'linkedList< int> :: dummyNode'c ++ [英] invalid use of non-static data member '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:
- 命名空间含义:
linkedlist :: iterator
大于linkedlist_iterator
- 根据C ++版本标准,嵌套类有实例的成员对象的额外能见度外部阶级
- 也许其他我不知道的事情
- Namespace meaning:
linkedlist::iterator
is more meaningful thanlinkedlist_iterator
- Depending on the C++ version standard, a nested class has extra visibility of the member objects of instances of the outer class
- 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< int> :: dummyNode'c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!