Compiler error in declaring template friend class within a template class

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 00:57:10

try adding a forward declaration

template <class T> class List;

at the start of Iterator.h -- that might be what you need to allow the friend declaration inside the Iterator class to work.

The problem is List has not been properly declared in Iterator.h. Instead, nest the Iterator class inside List (automagically making it a template), which you'll likely want to do anyway (to use List::Iterator instead of renaming it to ListIterator or IteratorForList, as you would to have more than one Iterator in a namespace).

template<class T>
struct List {
  //...
  struct Node {/*...*/};
  struct Iterator {
    // ...
  private:
    Iterator(Node*);
    friend class List; // still required
  };
  //...
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!