Compiler error in declaring template friend class within a template class

前端 未结 2 1896
名媛妹妹
名媛妹妹 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
    struct List {
      //...
      struct Node {/*...*/};
      struct Iterator {
        // ...
      private:
        Iterator(Node*);
        friend class List; // still required
      };
      //...
    };
    

提交回复
热议问题