0

This is the code for a simple binary tree program:

 #include <iostream>
 using namespace std;

template <class Tree>

class node
{
public:
    node()
    {
        this->tree=NULL;
        this->leftnode=this->rightnode=NULL;

    };

    ~node();

    node(Tree* base)
    {
        this->tree=base;
        this->leftnode=this->rightnode=NULL;
    };

    Tree* getnodevalue()
    {
        return this->tree;
    };

    node* getleftnode()
    {
        return leftnode;
    };

    node* getrightnode()
    {
        return rightnode;
    };

    void newleft(node* val)
    {
        this->leftnode=val;
    };

    void newright(node* val)
    {
        this->rightnode=val;
    };

    void newnode(Tree* val)
    {
        this->tree=val;
    };

    int chkleaf()
    {
        if(this->leftnode==NULL && this->rightnode==NULL)
            return 1;
        return 0;
    }

    void insert(node* nextnode, int* value)
    {
        node <int>* nodecounter= new node<int>(value);
        node<int> *p, *q;
        p=q=nextnode;

        while(*value!=*(p->getnodevalue()) && q!=NULL)
        {
            p=q;
            if(*value<*(p->getnodevalue()))
                q=p->getleftnode();
            else
                q=p->getrightnode();

        };

        if(*value==*(p->getnodevalue()))
        {
            cout<<"Duplicate"<<*value<<"\n";
            delete nodecounter;
        }
        else if(*value<*(p->getnodevalue()))
            p->newleft(nodecounter);
        else
            p->newright(nodecounter);
    };


    private:
        Tree* tree;
        node* leftnode;
        node* rightnode;
};


int main(int argc, char* argv[])
 {
          int newtree[5]={0,45,67,34,24};
          node<int>* node1= new node<int>();
          node1->newnode(&newtree[0]);

    for(int i=1; newtree[i]>0; i++)
    {
      node1->insert(node1, &newtree[i]);
    };  
    return 0;
 }

I can't understand what the issue is, I've never had this error before. Is it a coding problem, or something else? Would really appreciate some insight

Skyward
  • 3
  • 1
  • 1
    What does the compiler (linked in this case) saids exactly? – Bartosz Przybylski Nov 30 '16 at 06:06
  • 1
    @Skyward Surely `ld` also gave you a more detailed error message? – Biffen Nov 30 '16 at 06:06
  • 1
    Could it be the missing destructor? – Biffen Nov 30 '16 at 06:07
  • @Bartosz : This is the exact message – Skyward Nov 30 '16 at 06:10
  • @Biffen: Nope, or maybe I don't know where to look. C:\Users\HnH\AppData\Local\Temp\ccZUO5h8.o:Complete Binary tree.cpp:(.text$_ZN4nodeIiE6insertEPS0_Pi[_ZN4nodeIiE6insertEPS0_Pi]+0x11e): undefined reference to `node::~node()' collect2.exe: error: ld returned 1 exit status – Skyward Nov 30 '16 at 06:11
  • 1
    @Skyward The message is indeed about the missing destructor. – Biffen Nov 30 '16 at 06:11
  • There is a destructor. – Skyward Nov 30 '16 at 06:11
  • 1
    @Skyward No, there is a destructor *definition*, but no implementation. – Biffen Nov 30 '16 at 06:12
  • @Skyward as @Biffen said its about missing destructor, change `~node();` to `~node() {}` or `~node() = default;` – Bartosz Przybylski Nov 30 '16 at 06:12
  • Tht did it, just added the implementation. Thank you :):) – Skyward Nov 30 '16 at 06:14
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Biffen Nov 30 '16 at 06:17
  • Use a free software compiler like [GCC](http://gcc.gnu.org/) or [Clang](http://clang.llvm.org/) and compile on the command line with all warnings & debug info (e.g. `g++ -Wall -g`). Learn about [make](https://www.gnu.org/software/make/) – Basile Starynkevitch Nov 30 '16 at 06:17
  • Possible duplicate of [C compile error: Id returned 1 exit status](http://stackoverflow.com/questions/17717946/c-compile-error-id-returned-1-exit-status) – Raams Nov 30 '16 at 06:20

1 Answers1

0

Linker complains about missing destructor definition.

Change

node();

to

~node() {}

or, if you are using c++11

~node() = default;

You also need to release used memory in destructor using delete statement.

Bartosz Przybylski
  • 1,628
  • 10
  • 15
  • Thank you for solving this so quickly. Do I just delete the pointers? – Skyward Nov 30 '16 at 06:20
  • If `node` is the only owner of those pointers than yes. Otherwise read about owership in C++ eg here: http://stackoverflow.com/questions/94227/smart-pointers-or-who-owns-you-baby – Bartosz Przybylski Nov 30 '16 at 06:22