linker will nicht static class funktionen linken



  • Ich habe eine solche class:

    template <class DataType>
    class TAvlNode {
    private:
      TKeyType<DataType> *data;
    //...
    public:
      static TKeytype<DataType> *Insert(TKeytype<DataType> *newData, TAvlNode<DataType>* &root, short& change);
      static TKeytype<DataType> *Search(TKeytype<DataType> &key, TAvlNode<DataType>* &root, TCmp cmp);
    //...
    };
    

    Und dann in code steht:

    short change;
      // class TLabelNode: public TKeyType<TLabel>; (struct TLabel;)
      TLabelNode *newLabel = new TLabelNode(firstUndef, lName, line, col, lDefined, lValue);
      if (!TAvlNode<TLabel>::Insert(newLabel,firstNode,change)) return -1;
      else return 0;
    

    Aber linker gibt mir solche error:

    g++ mnem_list.o label.o struct.o errs_list.o scanner.o parser.o set.o stack.o evaluator.o main.o -o hc11asm
    label.o: In function `CLabelTable::newLabel(char const*, unsigned long, unsigned long, bool, unsigned short)':
    /home/anton/hc11/label.C:77: undefined reference to `TAvlNode<TLabel>::Insert(TKeytype<TLabel>*, TAvlNode<TLabel>*&, short&)'
    label.o: In function `CLabelTable::valueOf(char const*)':
    /home/anton/hc11/label.C:84: undefined reference to `TAvlNode<TLabel>::Search(TKeytype<TLabel>&, TAvlNode<TLabel>*&, TCmp)'
    label.o: In function `CLabelTable::labelDefined(char const*)':
    /home/anton/hc11/label.C:90: undefined reference to `TAvlNode<TLabel>::Search(TKeytype<TLabel>&, TAvlNode<TLabel>*&, TCmp)'
    label.o: In function `CLabelTable::defineValueOf(char const*, unsigned short)':
    /home/anton/hc11/label.C:96: undefined reference to `TAvlNode<TLabel>::Search(TKeytype<TLabel>&, TAvlNode<TLabel>*&, TCmp)'
    collect2: ld returned 1 exit status
    

    Und non-static funktionen können gelinkt worden, nur mit den static funktionen sind probleme. 😕
    Compiler ist gcc 3.2.
    Was mache ich falsch?



  • zeig mal die implementation der static methoden.
    (funktionskopf reicht)



  • Shade Of Mine schrieb:

    zeig mal die implementation der static methoden.
    (funktionskopf reicht)

    Zum Beispiel, implementation der TAvlNode<DataType>::Insert Funktion ist:

    template <class DataType>
    TKeytype<DataType> *TAvlNode<DataType>::Insert(TKeytype<DataType> *newData,
    					       TAvlNode<DataType>* &root, short& change) {
      // if root is empty, newData can be inserted
      if (root == NULL) {
        root = new TAvlNode<DataType>(newData);
        change = HEIGHT_CHANGE;
        return NULL;
      }
      short increase = 0;
      TCmp result = root->Compare(item->Key);
      if (result != CMP_EQUAL) {
        TKeytype<DataType> *found = Insert(newData, root->subNode[(result==CMP_LESS)?LEFT:RIGHT],change);
        if (found) return found;
        increase = result * change;
        root->balance += increase;
        change = (increase && root->balance ?(1 - Rebalance(root)):HEIGHT_NOCHANGE);
        return NULL;
      // item already exists
      } else {
        return newData;
      }
    }
    

    Aber hier sehe ich kein Problem. (alles soll schon Compiler melden, stimmt?)
    Ich habe wirklich keine Ahnung, was ist falsch.



  • template <class DataType> 
    TKeytype<DataType> *TAvlNode<DataType>::Insert(TKeytype<DataType> *newData, 
                               TAvlNode<DataType>* &root, short& change) { 
    ....
    

    Steht das in einer Datei die im Header includiert wird oder in einem .cpp File ?



  • template <class DataType> 
    class TAvlNode { 
    private: 
      TKeyType<DataType> *data; 
      ^^^^^^^^
    //... 
    public: 
      static TKeytype<DataType> *Insert(TKeytype<DataType> *newData, TAvlNode<DataType>* &root, short& change); 
             ^^^^^^^^                   ^^^^^^^^                                            
      static TKeytype<DataType> *Search(TKeytype<DataType> &key, TAvlNode<DataType>* &root, TCmp cmp); 
             ^^^^^^^^                   ^^^^^^^^
    //... 
    };
    
    g++ mnem_list.o label.o struct.o errs_list.o scanner.o parser.o set.o stack.o evaluator.o main.o -o hc11asm 
    label.o: In function `CLabelTable::newLabel(char const*, unsigned long, unsigned long, bool, unsigned short)': 
    /home/anton/hc11/label.C:77: undefined reference to `TAvlNode<TLabel>::Insert(TKeytype<TLabel>*,TAvlNode<TLabel>*&, short&)' 
                                                                                  ^^^^^^^^
    
    label.o: In function `CLabelTable::valueOf(char const*)': 
    /home/anton/hc11/label.C:84: undefined reference to `TAvlNode<TLabel>::Search(TKeytype<TLabel>&, TAvlNode<TLabel>*&, TCmp)' 
                                                                                  ^^^^^^^^
    
    label.o: In function `CLabelTable::labelDefined(char const*)': 
    /home/anton/hc11/label.C:90: undefined reference to `TAvlNode<TLabel>::Search(TKeytype<TLabel>&, TAvlNode<TLabel>*&, TCmp)' 
                                                                                  ^^^^^^^^
    
    label.o: In function `CLabelTable::defineValueOf(char const*, unsigned short)': 
    /home/anton/hc11/label.C:96: undefined reference to `TAvlNode<TLabel>::Search(TKeytype<TLabel>&, TAvlNode<TLabel>*&, TCmp)' 
                                                                                  ^^^^^^^^
    
    collect2: ld returned 1 exit status
    

    Absicht?
    Einmal "TKeyType" und dann "TKeytype".



  • Eine gute Frage, Knuddlbaer! Ich möchte, dass compiler mir template Funktionen übersetzt, wenn es noch nicht für welchen Typ weiss!
    Und dann sind implementiationen der template Methoden einfach übergeht.

    Jetzt habe ich auch implementiationen der Methoden in Header-file getragen und es funktioniert.

    Ich arbeite mit template sowieso zu wenig und dann mache ich solche Dummheiten ...


Anmelden zum Antworten