Spezielles Problem mit Templates und friend Klassen
-
Hi Leute!
Ich habe ein Problem bezüglich Templates und friend KLassen. Ich weiß momentan nicht recht weiter. Hier das Problem: Ich habe 2 C++ Template Klassen geschrieben und möchte die eine Template Klasse als friend Klasse der anderen Klasse deklarieren. Hier einmal die Schnittstellen der Klassen:
#ifndef NODE_H #define NODE_H #include <iostream> #include <assert.h> using namespace std; template <class T> class Node { protected: T *element; Node<T> *next; public: Node(); Node(const T &); Node(const Node<T> &); virtual ~Node(); virtual void setElement(const T &); virtual T getElement() const; virtual void setNext(Node<T> *); virtual Node<T> *getNext() const; Node<T> & operator=(const Node<T> &); bool operator==(const Node<T> &); bool operator!=(const Node<T> &); }; ... Implementierung
Die nächste Klasse: #ifndef SET_H #define SET_H #include "node.cpp" #include "assert.h" #include <iostream> template <class T> class Set{ protected: int size; Node<T> *head; public: Set(); Set(const T [], int); Set(const Set<T> &); virtual ~Set(); virtual Node<T> *getHead() const; virtual bool isEmpty() const; virtual int getSize() const; virtual void put(const T &); virtual bool contains(const T &) const; virtual Set<T> & intersect(const Set<T> &); virtual Set<T> & unite(const Set<T> &); Set<T> & operator=(const Set<T> &); }; ... Implementierung
Jetzt möchte ich natürlich in der Klasse Node<T> deklarieren, dass die Klasse Set<T> eine friend Klasse der Klasse Node<T> ist, um effizient auf die geschützten Variablen zugreifen zu können. Schreib ich jedoch:
template <class T> class Node { friend class Set<T> ....
bekomme ich folgende Fehlermeldungen:
node.cpp:12: error:Set' is not a template node.cpp:12: error: ISO C++ forbids declaration of
type name' with no type
node.cpp:12: error:Set' is not a template node.cpp:12: error: ISO C++ forbids declaration of
type name' with no type
In file included from set.cpp:4:
node.cpp:12: error:Set' is not a template node.cpp:12: error: ISO C++ forbids declaration of
type name' with no typeDiese Fehlermeldungen leuchten mir auch ein, da ich Set.cpp nicht inkludiere.
Füge ich jedoch ein include "set.cpp" hinzu, bekomme ich extrem viele Compilerfehler. Ich bin momentan echt ratlos. Wäre toll wenn mir jemand helfen könnte. Ich verwende übrigens den g++ 3.3.3 für cygwin.lg.
Rupert
-
Eine Template Klasse muss als Template friend spezifiziert werden, ähnliche Probleme hatte ich letztens auch.
template <class T1> friend class Set;
btw:
Hast du schon mal daran gedacht, Node bzw. Set als nested class zu implementieren?