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
In your
main-function you are declaring aList<int>and anList<int>::Iterator. Where are you callingbegin()? Onl1, which is aList<int>.Does
List<int>have a definition ofbegin()? No. This function is defined inList<int>::Iterator.I think you have a misunderstanding about the meaning of
friendhere. It means, the friend class (List<T>in this case) is allowed to accessprivateorprotectedmembers of the class (Iteratorin this case).It does not make
List<T>::headmagically available inIterator. It does not makeIterator::beginaccessible on an instance ofList<T>.I suggesst the following small change: move the declaration of
beginfromIteratortoList<T>and provide an appropriate constructor forIterator. You can then remove any friend-declarations, as they are not needed.