lundi 28 décembre 2020

Compiler Error in C++: ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) [duplicate]

I tried to write the code to manage a search binary tree, but why the compiler gives me the following error?

Undefined symbols for architecture x86_64: "ABR::printASCIITree()", referenced from: _main in ABRMain-4a2894.o "Node::Node(int)", referenced from: _main in ABRMain-4a2894.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

this is the code:

ABRMain.cpp:

#include <iostream>
#include "ABR.hpp"

using namespace std;

int main(){
    ABR<int> *tree = new ABR<int>(new Node<int>(5));

    tree->printASCIITree();
}

Node.hpp:

#include <iostream>

template <typename T>
class Node{
    private:
        T key;
        Node<T> *parent;
        Node<T> *right;
        Node<T> *left;
    public:
        Node();
        Node(T);

        T getKey();
        void setKey(T);

        void setParent(T*);
        T *getParent();

        void setRight(T*);
        T *getRight();

        void setLeft(T*);
        T *getLeft();
};

Node.cpp:

#include "Node.hpp"

using namespace std;

template <typename T>
Node<T>::Node(){
    parent = nullptr;
    right = nullptr;
    left = nullptr;
}

template <typename T>
Node<T>::Node(T k){
    key = k;
    parent = nullptr;
    right = nullptr;
    left = nullptr;
}

template <typename T>
T Node<T>::getKey(){
    return key;
}

template <typename T>
void Node<T>::setKey(T k){
    key = k;
}

template <typename T>
void Node<T>::setParent(T *f){
    parent = f;
}

template <typename T>
T *Node<T>::getParent(){
    return parent;
}

template <typename T>
void Node<T>::setRight(T *r){
    right = r;
}

template <typename T>
T Node<T>::*getRight(){
    return right;
}

template <typename T>
T Node<T>::*setLeft(T *l){
    left = l;
}

template <typename T>
T Node<T>::*getLeft(){
    return left;
}

ABR.hpp:

#include <iostream>
#include "Node.hpp"

using namespace std;

template <typename T>
class ABR{
    private:
        Node<T> *root;
        void transplant(Node<T> *, Node<T> *);
    public:
        ABR():root(nullptr){}
        ABR(Node<T> *r):root(r){}

        Node<T> *search(T);
        Node<T> *search(Node<T> *, T);

        Node<T> *minimum();
        Node<T> *minimum(Node<T> *);

        Node<T> *maximum();
        Node<T> *maximum(Node<T> *);

        void insert(T);

        Node<T> *successor(Node<T> *);
        Node<T> *predecessor(Node<T> *);
        void deleteNode(Node<T> *);

        void printASCIITree();
        void printASCIITree(Node<T> *, int);
};

ABR.cpp:

#include "ABR.hpp"

using namespace std;

template <typename T>
Node<T> *ABR<T>::search(T key){
    return search(root, key);
}

template <typename T>
Node<T> *ABR<T>::search(Node<T> *node, T key){
    if(node == nullptr || node->getKey() == key) return node;

    if(node->getKey() > key) 
        return search(node->getRight(), key);
    else 
        return search(node->getLeft(), key);
}

template <typename T>
Node<T> *ABR<T>::minimum(){
    return minimum(root);
}

template <typename T>
Node<T> *ABR<T>::minimum(Node<T> *node){
    if(node->getLeft() == nullptr) return node;

    minimum(node->getLeft());
}

template <typename T>
Node<T> *ABR<T>::maximum(){
    return maximum(root);
}

template <typename T>
Node<T> *ABR<T>::maximum(Node<T> *node){
    if(node->getRight() == nullptr) return node;

    minimum(node->getRight());
}

template <typename T>
void ABR<T>::insert(T key){
    Node<T> *y = nullptr;
    Node<T> *pt = root;

    while(pt != nullptr){
        y = pt;
        if(pt->getKey() < key)
            pt = pt->getLeft();
        else
            pt = pt->getRight();
    }

    Node<T> *z = new Node<T>(key);
    z->setFather(y);
    if(y == nullptr)
        root = z;
    else if(z->getKey() < y->getKey())
        y->setLeft(z);
    else
        y->setRight(z);
}

template <typename T>
Node<T> ABR<T>::*successor(Node<T> *node){

    if(node->getRight() != nullptr)
        return minimum(node->getRight());

    Node<T> *pt = node->getParent();

    while(pt != nullptr && node == pt->getRight()){
        node = pt;
        pt = pt->getParent();
    }

    return pt;
}

template <typename T>
Node<T> ABR<T>::*predeecessor(Node<T> *node){

    if(node->getLeft() != nullptr)
        return maximum(node->getLeft());

    Node<T> *pt = node->getParent();

    while(pt != nullptr && node == pt->getLeft()){
        node = pt;
        pt = pt->getParent();
    }

    return pt;
}

template <typename T>
void ABR<T>::deleteNode(Node<T> *node){

    if(node->getLeft() == nullptr){
        transplant(node, node->getRight());
    }else if(node->getRight() == nullptr){
        transplant(node, node->getLeft());
    }else{
        Node<T> y = minimum(node->getRight());
        if(y->getParent() != node){
            transplant(y, y->getRight());
            y->setRight(node->getRight());
            node->getRight()->setParent(y);
        }
        transplant(node, y);
        y->setLeft(node->getLeft());
        y->getLeft()->setParent(y);
    }
    
}

template <typename T>
void ABR<T>::transplant(Node<T> *u, Node<T> *v){

    if(u->getParent() == nullptr){
        root = v;
    }

    else if(u == u->getParent()->getLeft()){
        u->getParent()->setLeft(v);
    }

    else if(u == u->getParent()->getRight()){
        u->getParent()->setRight(v);
    }

    if(v != nullptr){
        v->setFather(u->getParent());
    }
}

template <typename T>
void ABR<T>::printASCIITree(){
    printASCIITree(root, 0);
}

template <typename T>
void ABR<T>::printASCIITree(Node<T> *node, int level){
    if(node == nullptr) return;

    for(int j=0; j<level; j++){
        cout << "|";
        if(j == level-1)
            cout << "-";
    }

    cout << node->getKey() << endl;

    printASCIITree(node->getLeft(), level+1);
    printASCIITree(node->getRight(), level+1);
}

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire