Błąd kompilatora w deklarowaniu klasy znajomego szablonu w klasie szablonu
Próbowałem wdrożyć własną, połączoną klasę list do celów dydaktycznych.
Podałem klasę „List” jako przyjaciela wewnątrz deklaracji Iteratora, ale wydaje się, że nie kompiluje się.
Oto interfejsy 3 klas, których użyłem:
Node.h:
#define null (Node<T> *) 0
template <class T>
class Node {
public:
T content;
Node<T>* next;
Node<T>* prev;
Node (const T& _content) :
content(_content),
next(null),
prev(null)
{}
};
Iterator.h:
#include "Node.h"
template <class T>
class Iterator {
private:
Node<T>* current;
Iterator (Node<T> *);
public:
bool isDone () const;
bool hasNext () const;
bool hasPrevious () const;
void stepForward ();
void stepBackwards ();
T& currentElement () const;
friend class List<T>;
};
List.h
#include <stdexcept>
#include "Iterator.h"
template <class T>
class List {
private:
Node<T>* head;
Node<T>* tail;
unsigned int items;
public:
List ();
List (const List<T>&);
List& operator = (const List<T>&);
~List ();
bool isEmpty () const {
return items == 0;
}
unsigned int length () const {
return items;
}
void clear ();
void add (const T&);
T remove (const T&) throw (std::length_error&, std::invalid_argument&);
Iterator<T> createStartIterator () const throw (std::length_error&);
Iterator<T> createEndIterator () const throw (std::length_error&);
};
I to jest program testowy, który próbowałem uruchomić:
trial.cpp
using namespace std;
#include <iostream>
#include "List/List.cc"
int main ()
{
List<int> myList;
for (int i = 1; i <= 10; i++) {
myList.add(i);
}
for (Iterator<int> it = myList.createStartIterator(); !it.isDone(); it.stepForward()) {
cout << it.currentElement() << endl;
}
return 0;
}
Kiedy próbuję go skompilować, kompilator podaje następujące błędy:
Iterator.h: 26: błąd: „Lista” nie jest szablonem
Iterator.h: W instancji „Iterator”:
trial.cpp: 18: instancja pochodzi stąd
Iterator.h: 12: error: wymagany jest argument szablonu dla „struct List”
List.cc: W funkcji członka „Iterator List :: createStartIterator () const [z T = int]”:
trial.cpp: 18: instancja pochodzi stąd
Iterator.h: 14: error: „Iterator :: Iterator (Node *) [with T = int]” jest prywatny
List.cc:120: error: w tym kontekście
Wygląda na to, że nie rozpoznaje deklaracji przyjaciela. Gdzie popełniłem błąd?