0
 template <class T>
class List
{
class Node
{
public:
    Node()
    {
        next = 0;
        data = 0;
    }
    Node* next;
    T data;
};
Node* head = new Node;
Node* tail = new Node;
int size = 0;
public:
class Iterator
{
 Node* curr;
    friend class List<T>;
public:
 Iterator()
    {
        curr = nullptr;
    }
    friend void fun()
    {
        cout << "helloworld" << endl;
    }
    Iterator begin()
    {
        Iterator it(head->next);
        return it;
    }
};
};

created two more class of blocks and programs, programs contained a list of blocks. Implemented iterators for ease of use, But am not able to access its public and private members through list class.

int main()
{
List<int> l1;
List<int>::Iterator it;
it = l1.begin();
fun();//iterator befriending neither class nor function
}

Error was: class List has no member begin E0135 begin: is not a member of class list C2039 On vs22

ppp
  • 1
  • 1
  • 1
    Please show a [mre]. What functions were not accesible? What error did you get? Where and how did you use `Iterator`? Also the title of your question should _summarize_ the problem, it shouldn't contain half of you question, because it's length is limited as you see. – Lukas-T Sep 16 '22 at 07:04
  • sorry, was my first time asking a question here – ppp Sep 16 '22 at 07:10
  • int main() { List l1; List::Iterator it; it = l1.begin(); } – ppp Sep 16 '22 at 07:10
  • thiss gives an error that link doesn't have the function begin – ppp Sep 16 '22 at 07:11
  • There is not `List::begin()` method in the code you posted. Please post some code that reproduces the problem and the complete error message. Read about [mcve] – 463035818_is_not_an_ai Sep 16 '22 at 07:16
  • @churill `Iterator begin(){Iterator it(head->next);return it;} – ppp Sep 16 '22 at 07:16
  • @HassanAhmad Dont' addd bits and pieces in the comments. Post a [mcve]. – molbdnilo Sep 16 '22 at 07:20
  • 2
    @HassanAhmad Thanks for adding more information :) It seems the `begin`-method should be a member of `List`, not of `Iterator`. – Lukas-T Sep 16 '22 at 07:29
  • thanks but even if i try making a friend function in iterator class it is not accessible outside its scope. Would appreciate it if you knew the error.@churill – ppp Sep 16 '22 at 07:31
  • @HassanAhmad If you are asking about friend functions in the `Iterator` class, then show some code with a friend function in the `Iterator` class. No-one can fix code you have not shown. – john Sep 16 '22 at 07:52
  • @john added the function – ppp Sep 16 '22 at 08:04
  • @HassanAhmad Apart from the `begin` problem (see comments above and answer below) you just need to add `extern void fun();` before `main`. You can also remove `friend class List;` which is not necessary. – john Sep 16 '22 at 08:22
  • @HassanAhmad Here's a [duplicate question](https://stackoverflow.com/questions/7785886/access-friend-function-defined-in-class) – john Sep 16 '22 at 08:26
  • Does this answer your question? [Access friend function defined in class](https://stackoverflow.com/questions/7785886/access-friend-function-defined-in-class) – inarighas Sep 19 '22 at 14:28

1 Answers1

0

In your main-function you are declaring a List<int> and an List<int>::Iterator. Where are you calling begin()? On l1, which is a List<int>.

List<int> l1;
List<int>::Iterator it;
//   VV -> That's the List<int>
it = l1.begin();

Does List<int> have a definition of begin()? No. This function is defined in List<int>::Iterator.

I think you have a misunderstanding about the meaning of friend here. It means, the friend class (List<T> in this case) is allowed to access private or protected members of the class (Iterator in this case).

It does not make List<T>::head magically available in Iterator. It does not make Iterator::begin accessible on an instance of List<T>.


I suggesst the following small change: move the declaration of begin from Iterator to List<T> and provide an appropriate constructor for Iterator. You can then remove any friend-declarations, as they are not needed.

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • Thanks was really mistaking friend class. – ppp Sep 16 '22 at 08:27
  • @HassanAhmad Glad it helped you. If your question is now answered, please [accept](https://stackoverflow.com/help/accepted-answer) the answer to mark the question as resolved. – Lukas-T Sep 19 '22 at 14:44