Question

I've got an "Undefined symbols for architecture x86_64" and can't seem to understand the reason. Here's the header file:

#include <string>
#include <iostream>
#include <iomanip>
#include "assert.h"
using namespace std;

template <class Etype>
class AvlNode {
public:
    Etype element;
    AvlNode *parent;
    AvlNode *left;
    AvlNode *right;
    int height;
    AvlNode(Etype e,AvlNode *lt, AvlNode *rt, AvlNode *p,int h=0)
    : element(e), left(lt), right(rt), parent(p), height(h) {}
};

template <class Etype>
class AvlTree {
public:
    AvlTree() {root=NULL;}
    ~AvlTree() {makeEmpty();}

    void makeEmpty() {makeEmpty(root);}
    bool remove(Etype x) {return remove(root,x);}
    void insert(Etype x) {return insert(x,root,NULL);}

    bool tooHeavyLeft(AvlNode<Etype> * t);
    bool tooHeavyRight(AvlNode<Etype> * t);
    bool heavyRight(AvlNode<Etype> * t);
    bool heavyLeft(AvlNode<Etype> * t);

protected:
    AvlNode<Etype> *root;

    void makeEmpty(AvlNode<Etype> *& t);
    int height(AvlNode<Etype> *t);

    bool remove(AvlNode<Etype> *& t,Etype word);
    void insert(Etype x,AvlNode<Etype> *& t,AvlNode<Etype> *prev);

    void rotateWithLeftChild(AvlNode<Etype> *& t);
    void rorateWithRightChild(AvlNode<Etype> *& t);
    void doubleWithLeftChild(AvlNode<Etype> *& t);
    void doubleWithRightChild(AvlNode<Etype> *& t);
};

And here's the source file:

#include "AvlTree.h"

template <class Etype>
void AvlTree<Etype>::makeEmpty(AvlNode<Etype> *& t) {
    if(t!=NULL) {
        makeEmpty(t->left);
        makeEmpty(t->right);
        delete t;
    }
    t=NULL;
}

template <class Etype>
void AvlTree<Etype>::rotateWithLeftChild(AvlNode<Etype> *&t) {
    assert(t!=NULL && t->left !=NULL);
    AvlNode<Etype> *temp = t->left;
    t->left = temp->right;
    temp->right = t;
    t->height = max( height( t->left ), height( t->right ) ) + 1;
    temp->height = max( height( temp->left ), temp->height ) + 1;
    t = temp;
}

template <class Etype>
int AvlTree<Etype>::height(AvlNode<Etype> *t) {
    return t==NULL ? -1 : t->height;
}

And here's the error I'm getting:

Undefined symbols for architecture x86_64:
  "AvlTree<int>::makeEmpty(AvlNode<int>*&)", referenced from:
      AvlTree<int>::makeEmpty() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Can you please find out the issue?

Thanks

Edit: I just copied the contents of the source file to the header file and the project compiled. That's great, however, I'd be very grateful if somebody explained to me the reason for that error, because I have no idea.

Était-ce utile?

La solution

The reason for the error is that you should always put all template code in header files. Move all the code you have in AvlTree.cpp to AvlTree.h (and make the functions inline). Delete AvlTree.cpp. Linkers cannot link template code, it has to be in header file so the compiler can see the definitions. See here for an explanation.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top