C++ -- 使用嵌套类中的类元素?
C++ -- Using a class' elements from a nested class?
好的,我有一个类LinkedList,它有一个嵌套类LinkedListIterator。在LinkedListIterator的方法中,我引用了LinkedList的私有字段。我认为这是合法的。但我得到了错误:
from this location
每次我提到他们。
我在封闭类中的字段上得到了相应的错误消息:
invalid use of non-static data member 'LinkedList<int>::tail'
知道为什么吗?相关代码如下:
template<class T>
class LinkedList {
private:
//Data Fields-----------------//
/*
* The head of the list, sentinel node, empty.
*/
Node<T>* head;
/*
* The tail end of the list, sentinel node, empty.
*/
Node<T>* tail;
/*
* Number of elements in the LinkedList.
*/
int size;
class LinkedListIterator: public Iterator<T> {
bool add(T element) {
//If the iterator is not pointing at the tail node.
if(current != tail) {
Node<T>* newNode = new Node<T>(element);
current->join(newNode->join(current->split()));
//Move current to the newly inserted node so that
//on the next call to next() the node after the
//newly inserted one becomes the current target of
//the iterator.
current = current->next;
size++;
return true;
}
return false;
}
不能像那样只使用非static
成员。我认为下面的例子可以说明问题:
LinkedList<int>::LinkedListIterator it;
it.add(1);
方法中的current
和tail
是什么?没有LinkedList
的实例,所以这些成员甚至还不存在。
我并不是说让成员成为static
,这是错误的,但请重新考虑你的方法。
看看std
迭代器是怎样的。
相关文章:
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 不能使用嵌套结构中的联合元素作为 scanf() 的参数来存储所需的值
- 访问range_expression中的嵌套元素会返回不完整的映射(段错误)
- 删除嵌套向量中具有remove_if的元素
- 在循环访问嵌套向量时删除元素
- 为什么 OpenCL 嵌套循环仅适用于某些元素
- 嵌套的C 常数数据结构,没有初始化列表或明确命名所有节点-VC 2012过早破坏了内部元素
- C 无法使用嵌套环将数组的元素索引
- C++嵌套的带有擦除元素的循环
- 在嵌套向量C 中获取特定元素
- 标记元素C 阵列带嵌套循环
- 如何访问嵌套的stl元素
- 将xml字符串嵌套到xml元素中的语法
- C++ -- 使用嵌套类中的类元素?
- 解码元素的嵌套列表时使用ei_decode_list
- 如何在嵌套向量中获取元素类型
- 如何使用 qt 删除嵌套的 xml 元素
- 从向量生成元素的子集(无嵌套循环)
- 如何擦除嵌套在矢量中的列表中的元素
- 如何添加元素到嵌套向量