000
17.05.2004, 13:59 Uhr
~Spawn
Gast
|
Bei dem Code kommt es zu einem Linkerfehler unter g++, wenn die Spezialisierung des Templates für ein Char "exline" deklariert wird. Ist es überhaupt möglich die Spezialisierung in der cpp zu Deklarieren?
Die doubleList.h:
C++: |
/*-------------------------------------------------------Iterator Policy------------------------------------------------------- 1. es soll eine Subklasse mit Namen Iterator geben 2. es soll die beiden Methoden begin und end geben, die einen Iterator auf das erste bzw. letzte Element liefert 3. zwei Iteratoren sollen verglichen werden können, ob sie identisch sind 4. zu einem Iterator soll mittels des Prefix-Operators * das zugehörige Element ermittelt werden 5. zu einem Iterator soll mittels des Prefix-Operators ++ ein Iterator zurückgeliefert werden, der das nächste Element enthält -----------------------------------------------------------------------------------------------------------------------------*/
using namespace std;
template<class T> class doubleList{ public: class Node{ public: Node(T x):Elem(x), prev(0), next(0){}; T Elem; Node* prev; Node* next; };
class Iterator{ public: bool operator != (const Iterator&); bool operator == (const Iterator&); T operator * (); Iterator operator++(); Node* m_Elem; }; doubleList():head(0),tail(0),isEmpty(true){}; doubleList<T> operator | (const doubleList<T>&); doubleList<T> operator & (const doubleList<T>&); bool isEmpty; bool exists(T); void pushHead(T); void pushTail(T); void delNode(Node*); void popHead(); void popTail(); void printList(); Node* findElem(T); Iterator begin(); Iterator end(); private: Node* head; Node* tail; };
Die doubleList.cpp:
#include "doubleList.h"
template<class T> doubleList<T> doubleList<T>::operator | (const doubleList<T>& x){ doubleList<T>* z = new doubleList<T>; Node* tmp = head; while(tmp != 0){ z->pushTail(tmp->Elem); tmp = tmp->next; } tmp = x.head; while(tmp != 0){ z->pushTail(tmp->Elem); tmp = tmp->next; } return *z; };
template<class T> doubleList<T> doubleList<T>::operator & (const doubleList<T>& x){ doubleList<T>* z = new doubleList<T>; Node* tmp1 = head; Node* tmp2 = x.head; while(tmp1 != 0){ while(tmp2 != 0){ if(tmp1->Elem == tmp2->Elem) z->pushTail(tmp1->Elem); tmp2 = tmp2->next; } tmp1 = tmp1->next; tmp2 = x.head; } return *z; };
template<class T> bool doubleList<T>::Iterator::operator != (const typename doubleList<T>::Iterator& x){ return (x.m_Elem->Elem) != (m_Elem->Elem); };
template<class T> bool doubleList<T>::Iterator::operator == (const typename doubleList<T>::Iterator& x){ return (x.m_Elem->Elem) == (m_Elem->Elem); };
template<class T> T doubleList<T>::Iterator::operator *(){ return (m_Elem->Elem); };
template<class T> typename doubleList<T>::Iterator doubleList<T>::Iterator::operator++(){ Iterator toSend; toSend.m_Elem = m_Elem->next; return toSend; };
template<class T> void doubleList<T>::pushHead(T inVal){ if(!exists(inVal)){ Node* toEnter = new Node(inVal); if(isEmpty){ head = toEnter; tail = toEnter; isEmpty = false; } else{ head->prev = toEnter; toEnter->next = head; head = toEnter; } } };
template<class T> void doubleList<T>::pushTail(T inVal){ if(!exists(inVal)){ Node* toEnter = new Node(inVal); if(isEmpty) pushHead(inVal); else{ toEnter->prev = tail; tail->next = toEnter; tail = toEnter; } } }; template<class T> void doubleList<T>::printList(){ if(head != 0){ Node* tmp = head; while(tmp != 0){ cout << tmp->Elem << " "; tmp=tmp->next; } cout << endl; } }; template<class T> void doubleList<T>::popHead(){ if(!isEmpty){ Node* toDelete = head; head = head->next; if(head != 0) head->prev = 0; else cout << "Liste ist leer!" << endl; delete toDelete; if(head == 0) isEmpty = true; } cout << "Liste ist leer!" << endl; };
template<class T> void doubleList<T>::popTail(){ if(!isEmpty){ Node* toDelete = tail; tail = tail->prev; if(tail != 0) tail->next = 0; else cout << "Liste ist leer" << endl; delete toDelete; if(tail == 0) isEmpty = true; } cout << "Liste ist leer!" << endl; };
template<class T> bool doubleList<T>::exists(T toFind){ return (findElem(toFind) != 0) ? true : false; };
template<class T> typename doubleList<T>::Node* doubleList<T>::findElem(T toFind){ Node* tmp = head; while(tmp != 0){ if(tmp->Elem == toFind) return tmp; tmp = tmp->next; } return tmp; };
template<class T> void doubleList<T>::delNode(Node* toDelete){ if(!isEmpty && toDelete != NULL){ if(toDelete->prev == 0) popHead(); else if(toDelete->next == 0) popTail(); else{ toDelete->prev->next = toDelete->next; toDelete->next->prev = toDelete->prev; delete toDelete; } } };
template<class T> typename doubleList<T>::Iterator doubleList<T>::begin(){ Iterator toSend; toSend.m_Elem = head; return toSend; };
template<class T> typename doubleList<T>::Iterator doubleList<T>::end(){ Iterator toSend; toSend.m_Elem = tail; return toSend; };
Die charSet.h Datei:
using namespace std;
template<> class doubleList<char>{ public: void printList(); };
Die charSet.cpp Datei:
#include "charSet.h"
template<> void doubleList<class T>::printList<char>(){ cout << "Test" << endl; }
Die main Datei:
#include <iostream> #include <conio.h> #include <stdlib.h> #include <assert.h> #include "doubleList.cpp" #include "charSet.cpp"
int main(){ doubleList<char> x; x.printList(); system("Pause"); return 0; };
|
|