abstrakte Klasse und Template



  • Hallo,
    ohne Template klappt es wunderbar. Aber mit Template kommt die Fehlermeldung:
    Test.obj : error LNK2001: Nichtaufgeloestes externes Symbol "public: virtual void __thiscall Node<int>::SetParentLink(unsigned long)" (?SetParentLink@?$Node@H@@UAEXK@Z)

    Hier erstmals ohne Template:
    //Cell.h
    #ifndef _CELL_H
    #define _CELL_H

    #include <stdio.h>
    #include <iostream>
    #include <string>
    using namespace std;

    #define PSEUDO NULL

    class Cell
    {
    public:
    unsigned long mParentLink;

    Cell(void)
    {
    mParentLink = PSEUDO;
    }

    Cell(unsigned long aP)
    {
    mParentLink = aP;
    }

    virtual void SetParentLink(unsigned long);
    };
    #endif
    ---------------------------------------------------------------
    //Cell.cpp
    #include "Cell.h"

    void Cell::SetParentLink(unsigned long aL)
    {
    mParentLink = PSEUDO;
    }
    ---------------------------------------------------------------
    //Node.h
    #ifndef _NODE_H
    #define _NODE_H

    #include "Cell.h"

    class Node : public Cell
    {
    private:
    int key;

    public:
    Node( const int& aKey) : Cell(PSEUDO) { key = aKey; }

    void SetParentLink(unsigned long aL);
    };
    #endif
    ---------------------------------------------------------------
    //Node.cpp
    #include "Node.h"

    void Node::SetParentLink(unsigned long aL)
    {
    mParentLink = aL;
    }
    ---------------------------------------------------------------
    //Test.cpp
    #include "Node.h"
    int main()
    {
    Node test(5);

    return 0;
    }
    ---------------------------------------------------------------
    Funktioniert wunderbar!!!

    Und hier mit Template:
    ---------------------------------------------------------------
    Cell.h und Cell.cpp bleiben unverändert
    ---------------------------------------------------------------
    //Node.h
    #ifndef _NODE_H
    #define _NODE_H

    #include <fstream>
    #include "Cell.h"

    template <class T>
    class Node : public Cell
    {
    private:
    T key;

    public:
    Node( const T& aKey) : Cell(PSEUDO) { key = aKey; }

    void SetParentLink(unsigned long aL);
    };
    #endif
    ---------------------------------------------------------------
    //Node.cpp
    #include "Node.h"

    template<class T>
    void Node<T>::SetParentLink(unsigned long aL)
    {
    mParentLink = aL;
    }
    ---------------------------------------------------------------
    //Test.cpp
    #include "Node.h"
    int main()
    {
    Node<int> test(5);

    return 0;
    }

    Woran liegt das Problem?



  • der template-code muss vollständig im header stehen und nicht in der cpp.



  • Cool, es funktioniert tatsächlich. Super danke.


Anmelden zum Antworten