I'm feeling really silly as this is just a syntax detail, but I have not managed to find a decent example on the web or in my textbooks.
How do you define a template class declared outside the class as a friend? I'm working on a linked list container class, and defining my List class as a friend of Iterator, which looks something like this:
iterator.h
Code:
#include "node.h"
template <class T>
class Iterator
{
// declare class List as a friend somewhere?
public:
typedef *Node<T> nodePtr;
T& operator*();
bool operator==();
//etc
private:
nodePtr dataNode;
void setNext();
void setPrev();
void getNext();
// etc accessors/mutators for node manipulation
}
list.h
Code:
#include "iterator.h"
template <class T>
class List
{
public:
typedef Iterator<T> iterator;
void push_back(const T& val); // functions need access to Iterator's
void push_front(const T& val); // private methods to change sequence
void insert(iterator pos); // of list.
private:
iterator head;
iterator tail;
};
They're may be some syntax errors. I don't have the code in front of me, so I'm just typing this in notepad from memory.
I looked at the STL list header, and they got around this buy declaring their iterator inside the list function, but I'm not sure thats appropriate for this project. I found plenty of examples on how to declare a non-template class as a friend, and I experimented with possible ways of writing a template version, but my compiler didn't like any of them. In case it effects the template syntax, I'm currenty using MS VC++ 6.0 SP 4.
Thank you,
AH_Tze