0

Error Message:

/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status


struct node
{
  int key_value;
  node *left;
  node *right;
};

class btree
{
    public:
        btree();
        ~btree();

        void insert(int key);
        node *search(int key);
        void destroy_tree();

    private:
        void destroy_tree(node *leaf);
        void insert(int key, node *leaf);
        node *search(int key, node *leaf);

        node *root;
};

btree::btree()
{
  root=NULL;
}

void btree::destroy_tree(node *leaf)
{
  if(leaf!=NULL)
  {
    destroy_tree(leaf->left);
    destroy_tree(leaf->right);
    delete leaf;
  }
}

void btree::insert(int key, node *leaf)
{
  if(key< leaf->key_value)
  {
    if(leaf->left!=NULL)
     insert(key, leaf->left);
    else
    {
      leaf->left=new node;
      leaf->left->key_value=key;
      leaf->left->left=NULL;    //Sets the left child of the child node to null
      leaf->left->right=NULL;   //Sets the right child of the child node to null
    }
  }
  else if(key>=leaf->key_value)
  {
    if(leaf->right!=NULL)
      insert(key, leaf->right);
    else
       {
      leaf->right=new node;
      leaf->right->key_value=key;
      leaf->right->left=NULL;  //Sets the left child of the child node to null
      leaf->right->right=NULL; //Sets the right child of the child node to null
    }
  }
}

node *btree::search(int key, node *leaf)
{
  if(leaf!=NULL)
  {
    if(key==leaf->key_value)
      return leaf;
    if(key<leaf->key_value)
      return search(key, leaf->left);
    else
      return search(key, leaf->right);
  }
  else return NULL;
}

void btree::insert(int key)
{
  if(root!=NULL)
    insert(key, root);
  else
  {
    root=new node;
    root->key_value=key;
    root->left=NULL;
    root->right=NULL;
  }
}

node *btree::search(int key)
{
  return search(key, root);
}

void btree::destroy_tree()
{
  destroy_tree(root);
}

Can anyone tell me where this error message is coming from so I can attempt to fix it? I'm trying to understand nodes better by running programs using nodes, and this error message is thrown.

jww
  • 97,681
  • 90
  • 411
  • 885
Kodie Hill
  • 21
  • 1
  • 6
  • 2
    You are missing `main` function. It is `int main () { /*code*/ return 0 }` - http://en.wikipedia.org/wiki/Main_function –  Nov 16 '12 at 21:04
  • In C++, its `int main () { /*code*/ }` without the return. – ipc Nov 16 '12 at 21:13
  • Having a return is perfectly valid, but it needs a `;`! – Robᵩ Nov 16 '12 at 21:14
  • Possible duplicate of [crt1.o: In function \`\_start': - undefined reference to \`main' in Linux](http://stackoverflow.com/questions/11116399/crt1-o-in-function-start-undefined-reference-to-main-in-linux) – jww Feb 18 '15 at 03:06

1 Answers1

4

As the error message says you are missing a main function. Every program needs a main function, that's where the program starts (more or less). What were you expecting this program to do?

john
  • 85,011
  • 4
  • 57
  • 81
  • Hm, so the program I picked up was incomplete. Thank you. – Kodie Hill Nov 16 '12 at 21:12
  • Not only incomplete but not especially good either. Can't trust anything on the internet. Looking at code is one thing, but the only real way to learn is to write your own code. – john Nov 16 '12 at 21:13
  • @KodieHill This code just defines a tree. You can use it to make your own tree. You should probably write your own if it's homework though... – Sev08 Nov 16 '12 at 21:15
  • No homework, we just started on nodes and binary trees were mentioned off handedly. Pursuing an interest at this point. – Kodie Hill Nov 16 '12 at 21:26