Templates!!



  • HI leute!
    hab meine DLList fertig, will sie jedoch noch mit template <class dType> erweitern
    irgendwie funkt das aber nicht, compilieren tut ers einwandfrei nur bei der ausführung schreibt er sowas wie: nicht aufgelöste externe funktionen ....

    hab ich was übersehn?

    hier ein teil des quellcodes

    template <class dType>
    dLList<dType>::~dLList(){
        dLLNode* p = head;
        dLLNode* q;
    
            while(p){
            q = p;
            p = p->next;
            delete q;
        }
        head = 0; //need to make sure that the list knows it's empty
    }
    
    template <class dType>
    void dLList<dType>::insert(dtype x){
        dLLNode* p = head;
        dLLNode* newNode = new dLLNode(x);
    
            if(p == 0){
            head = newNode;
            return;
        }
    
        while(p->next)
            p = p->next;
    
        p->next = newNode;
            newNode->prev = p;
    }
    
    template <class dType>
    void dLList<dType>::print(){
        dLLNode* p = head;
    
            while(p){
            cout << p->data << endl;
            p = p->next;
        }
    }
    

    naja, ich sage dank!



  • Steht die Definition der DLL in einer .cpp ?

    Ein Template muss überall bekannt sein da der Typ wärend der Compilierzeit verwendet wird.

    d.h. das was jetzt in der cpp steht müsste mit in den header (als include z.B.)



  • die header datei sieht so aus:

    #ifndef dLList_h
    #define dLList_h
    
    #include <iostream>
    
    using namespace std;
    
    //typedef int dtype;
    
    template<class dtype> 
    class dLLNode{
    
    public:
        dLLNode();      
            dLLNode(dtype x);
    
        dLLNode* prev;
        dLLNode* next;
        dtype data;
    };
    
    template<class dtype>
    class dLList{
    
    public:
        dLList();
        ~dLList();
        void insert(dtype x);
            void removeHead();
            void removeTail();
        void remove(dtype x);
        void print();
        void printRev();
        bool search(dtype x);
    
            //set the Tail!
            void setTail();
            dtype getTail();
            dtype getHead();
    
        dLLNode* head;
            dLLNode* tail;
    };
    
    #endif
    

    was meinst du mit DLL?

    die fehlermeldungen:
    dlisttest.obj : error LNK2001: Nichtaufgeloestes externes Symbol "public: __thiscall dLList<int>::~dLList<int>(void)" (??1?dLList@H@@QAE@XZ) dlisttest.obj : error LNK2001: Nichtaufgeloestes externes Symbol "public: void __thiscall dLList::printRev(void)" (?printRev@?dLList@H@@QAEXXZ)
    dlisttest.obj : error LNK2001: Nichtaufgeloestes externes Symbol "public: void __thiscall dLList<int>::removeTail(void)" (?removeTail@?dLList@H@@QAEXXZ) dlisttest.obj : error LNK2001: Nichtaufgeloestes externes Symbol "public: void __thiscall dLList::removeHead(void)" (?removeHead@?dLList@H@@QAEXXZ)
    dlisttest.obj : error LNK2001: Nichtaufgeloestes externes Symbol "public: int __thiscall dLList<int>::getTail(void)" (?getTail@?dLList@H@@QAEHXZ) dlisttest.obj : error LNK2001: Nichtaufgeloestes externes Symbol "public: int __thiscall dLList::getHead(void)" (?getHead@?dLList@H@@QAEHXZ)
    dlisttest.obj : error LNK2001: Nichtaufgeloestes externes Symbol "public: void __thiscall dLList<int>::print(void)" (?print@?dLList@H@@QAEXXZ) dlisttest.obj : error LNK2001: Nichtaufgeloestes externes Symbol "public: void __thiscall dLList::insert(int)" (?insert@?dLList@H@@QAEXH@Z)
    dlisttest.obj : error LNK2001: Nichtaufgeloestes externes Symbol "public: __thiscall dLList<int>::dLList<int>(void)" (??0?$dLList@H@@QAE@XZ)
    Debug/dlist.exe : fatal error LNK1120: 9 unaufgeloeste externe Verweise



  • Öhm, mit DLL war die DLList gemeint.

    Zu schnell gelesen sry O🤡

    Du kannst ein Template nciht mehr in der cpp stehen lassen. ALl das was bei Dir jetzt noch in der cpp steht muß da wo die Liste eingesetzt werden per Include einbezogen werden.

    Schau mal hier:
    Template in 2 Dateien

    [edit]Smilies....[/edit]
    Smilies...

    [ Dieser Beitrag wurde am 06.05.2003 um 12:20 Uhr von Knuddlbaer editiert. ]



  • und jetzt, was soll ich jetzt machen? ich hab keinen plan!



  • Kann es sein, dass es nur unter linux läuft???



  • du musst das die deklaration und definition die im header packen



  • sprich:
    einfach am ende der header ein
    #include "dllist.cpp"
    schreiben 🙂

    und am besten .cpp in .inl oder .tmpl oder derartiges umbenennen, damit man sieht dass es keine eigene ÜE ist.


Anmelden zum Antworten