Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » C / C++ (WinAPI, Konsole) » spezialisierte Templates

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
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;
};

 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
18.05.2004, 17:40 Uhr
~Spawn
Gast


Für alle die es interessiert, hier die Lösung:

/*-------------------------------------------------------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
-----------------------------------------------------------------------------------------------------------------------------*/

C++:
#ifndef _DOUBLELIST_H
#define _DOUBLELIST_H
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 typename doubleList<T>::Iterator& x){
            return (x.m_Elem->Elem) != (m_Elem->Elem);
        };
        
        bool operator == (const typename doubleList<T>::Iterator& x){
            return (x.m_Elem->Elem) == (m_Elem->Elem);
        };
        
        T operator *(){
            return (m_Elem->Elem);
        };
        
        Iterator operator++(){
            Iterator toSend;
            toSend.m_Elem = m_Elem->next;
            return toSend;
        };
        
        Node* m_Elem;        
    };
    
        doubleList():head(0),tail(0),isEmpty(true){};
        
        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;
        };
        
        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;
        };
        
        bool isEmpty;
        bool exists(T toFind){
           return (findElem(toFind) != 0) ? true : false;
        };
        
        void addElem(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;
               }
           }
        };

        void 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;
              }
           }
        };

        void printList(){
           if(head != 0){
              Node* tmp = head;
              while(tmp != 0){
                 cout << tmp->Elem << "  ";
                 tmp=tmp->next;
              }
              cout << endl;
           }
        };
        
        Node* findElem(T toFind){
          Node* tmp = head;
          while(tmp != 0){
                if(tmp->Elem == toFind)
                    return tmp;
                tmp = tmp->next;
            }
              return tmp;
        };
        Iterator begin(){
            Iterator toSend;
            toSend.m_Elem = head;
            return toSend;
        };
        Iterator end(){
            Iterator toSend;
            toSend.m_Elem = tail;
            return toSend;
        };
    private:
        Node* head;
        Node* tail;
};

//----------------------------------Spezialisierung-------------------------------

template<>
class doubleList<char>{
public:
    class Iterator{
    public:
        bool operator != (const Iterator& x){
            return(x.ptr != ptr);
        };
        bool operator == (const Iterator& x){
            return(x.ptr == ptr);
        };
        char operator *(){
            return ptr;
        };
        
        char ptr;
    };
    
    doubleList():isEmpty(true){
        charArray = new bool[256];
        for(int i = 0; i < 256; ++i)
            charArray[i] = false;
    };
    
    doubleList<char> operator|(const doubleList<char>& x){
        doubleList<char> ret;
        for(int i = 0; i < 256; ++i)
            if((charArray[i])||(x.charArray[i]))
                ret.addElem(i);
        return ret;
    };
    
    doubleList<char> operator&(const doubleList<char>& x){
        doubleList<char> ret;
        for(int i = 0; i < 256; ++i)
            if((charArray[i])&&(x.charArray[i]))
                ret.addElem(i);
        return ret;
    };
    
    bool exists(char toFind){
        return (charArray[(int)toFind]);
    };

    void addElem(char toAdd){
        if(isEmpty)
            isEmpty = false;
        charArray[(int)toAdd] = true;
    };
    
    void delNode(char toDelete){
        charArray[(int)toDelete] = false;
    };
    
    void printList(){
        if(!isEmpty){
            for(int i = 0; i < 256; ++i)
                if(charArray[i])
                    cout << (char)i << "  ";
        }
        else
            cout << "Liste ist leer";    
    };
    
    Iterator begin(){
        Iterator toSend;
        if(!isEmpty){
            for(int i = 0; i < 256; ++i)
                if(charArray[i]){
                    toSend.ptr = (char)i;
                    return toSend;
                }
        }
        else
            cout << "Liste ist leer";
    };
    
    Iterator end(){
        Iterator toSend;
        if(!isEmpty){
            for(int i = 255 ; i > -1; --i)
                if(charArray[i]){
                    toSend.ptr = (char)i;
                    return toSend;
                }
        }
        else
            cout << "Liste ist leer";    
    };
          
    private:
        bool* charArray;
        bool isEmpty;
};
    
#endif

 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ C / C++ (WinAPI, Konsole) ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: