Just plugged that into Dev-C++, although I switched the friend, so within Iterator class definition it declares "friend class List<T>".
Compiles with an "List is not a template" error. After looking at non-template friend examples, I think I need some placeholder code in iterator.h which tells the compiler a class List will be defined, but I'm not sure how to write this for a template class.
I wrote a quicky test program in which class List tries to access class Iterator's private member function. Hopefully this will illustrate my problem.
main.cpp
Code:
#include "list.h"
int main(int argc, char *argv[])
{
List<int> testList;
int testVal;
testVal = testList.getData();
system("PAUSE");
return 0;
}
list.h
Code:
#ifndef LIST_H
#define LIST_H
#include "iterator.h"
template <class T>
class List
{
private:
Iterator<T> data;
public:
T getData() {
return *(data.getdata()); // access private member of data
}
};
#endif
iterator
Code:
#ifndef ITERATOR_H
#define ITERATOR_H
template <class T>
class Iterator
{
//friend class List<T>; // compile error
private:
T* dataPtr;
T* getdata() {
return dataPtr;
}
public:
Iterator() {
dataPtr = NULL;
}
};
#endif