Compiler error in declaring template friend class within a template class

前端 未结 2 1897
名媛妹妹
名媛妹妹 2021-02-20 00:01

I have been trying to implement my own linked list class for didactic purposes.

I specified the \"List\" class as friend inside the Iterator declaration, but it doesn\'t

相关标签:
2条回答
  • 2021-02-20 00:47

    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
      };
      //...
    };
    
    0 讨论(0)
  • 2021-02-20 00:51

    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.

    0 讨论(0)
提交回复
热议问题