0

enter image description hereMy code that I recently build have this message :

clang: error: linker command failed with exit code 1 (use -v to see invocation)

after I build the whole project.

I have tried:

  • clean the build and build
  • I have saved my project file as workspace file and ran the file
  • In build settings there is no metaDataLevel so I can't flag it to None

Node.h

template<class dataType>
class Node {
private:
    dataType data;
    Node* next;
    Node* prev;
public:
    Node();
    Node(dataType);

    bool setNext(Node*);
    bool setPrev(Node*);
    Node* getNext() const;
    Node* getPrev() const;
    void setData(const dataType&);
    dataType getData() const;
    bool add(const dataType);
    bool remove(const dataType);

    ~Node();
};

template<class dataType>
Node<dataType>::Node()
{
    next = nullptr;
    prev = nullptr;
}

I am not sure if this will help or not but i'll place the implementation below:

template<class dataType>
Node<dataType>::Node(dataType d)
{
    data = d;
    next = nullptr;
    prev = nullptr;
}

template<class dataType>
bool Node<dataType>::setNext(Node* head)
{
    this->next = head;
    return true;
}

template<class dataType>
bool Node<dataType>::setPrev(Node* head)
{
    this->prev = head;
    return true;
}    

template<class dataType>
Node<dataType>* Node<dataType>::getNext() const
{
    return next;
}

template<class dataType>
Node<dataType>* Node<dataType>::getPrev() const
{
    return prev;
}

template<class dataType>
void Node<dataType>::setData(const dataType& d){
    this->data = d;
}

template<class dataType>
dataType Node<dataType>::getData() const
{
    return data;
}

template<class dataType>
bool Node<dataType>::add(const dataType d){
    Node* dummy = new Node(d);
    dummy->setNext(this);
    this->setPrev(dummy);
    dummy->setPrev(nullptr);
    this = dummy;
    return true;
}

template<class dataType>
bool Node<dataType>::remove(const dataType d){
    Node* pointer = this;
    while(pointer != nullptr)
    {
        if(pointer->getData() == d)
        {
            if(pointer->getPrev() == nullptr)
            {
                this = pointer->getNext();
                pointer = pointer->getNext();
                delete pointer->getPrev();
                pointer->setPrev(nullptr);
            }
            else if(pointer->getNext() == nullptr)
            {
                pointer->getPrev()->setNext(nullptr);
                delete pointer;
                pointer = nullptr;
            }
            else
            {
                pointer->getPrev()->setNext(pointer->getNext());
                pointer->getNext()->setPrev(pointer->getPrev());
                Node* dummy = pointer;
                pointer = pointer->getNext();
                delete dummy;
            }
        }
    }
    return true;
}

template<class dataType>
Node<dataType>::~Node(){
    Node* pointer = this;
    Node* holder = this->getNext();
    while (holder != nullptr)
    {
        delete pointer;
        pointer = holder;
        holder = holder->getNext();
    }
    delete pointer;
    delete holder;
}

here is the testing code i used:

int main(){
    int v = 10;
    string d = "X";
    Node<int>* headInt = new Node<int>(v);
    Node<string>* headString = new Node<string>();
    headString->setData(d);
    cout << "headInt contain" << headInt->getData() << ", ";
    cout << "headString contain" << headString->getData() << endl <<     endl;

    headInt->add(22);
    headString->add("XXII");
    cout << "headInt contain" << headInt->getData() << ", ";
    cout << "headString contain" << headString->getData() << endl << endl;

    headInt->remove(22);
    headString->remove("XXII");
    cout << "headInt contain" << headInt->getData() << ", ";
    cout << "headString contain" << headString->getData() << endl << endl;
}

I am expecting to have this as my result :

headInt contain 10,
headString contain X

headInt contain 22,
headString contain XXII

headInt contain 10,
headString contain X
  • 1
    Have you tried [this](https://stackoverflow.com/a/43801476/5517378) – NutCracker Oct 16 '19 at 21:36
  • 3
    There is almost always more error message than what's been provided. Find it and it might even tell you what you need to do. If not, add it to the question. – user4581301 Oct 16 '19 at 22:02
  • Please edit your post with the *complete* declaration of the `Node` class. The source file says it is a template class, but your snippet may show otherwise. – Thomas Matthews Oct 16 '19 at 22:13
  • @NutCracker not yet, i'll get to it asap. – Dan Dharta Oct 18 '19 at 15:15
  • @ThomasMatthews I am very sorry as I am fairly new to coding. What do you mean by snippet? I have figured that the problem is that I am unable to use a template class in my main program. So I am now looking for a way to allow me to use template in the main. Thank you though!! – Dan Dharta Oct 18 '19 at 22:32
  • @DanDharta: "Snippet" -- small portion of larger code. – Thomas Matthews Oct 18 '19 at 23:38

0 Answers0