Linker Error

This is a discussion on Linker Error within the C++ Programming forums, part of the General Programming Boards category; This code (from the tutorials) gives me a linker error. I'm using VC++ 6.0 Here's the code: #include <iostream.h> struct ...

  1. #1
    Unregistered
    Guest

    Linker Error

    This code (from the tutorials) gives me a linker error. I'm using VC++ 6.0

    Here's the code:

    #include <iostream.h>

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

    class btree
    {
    node *root;
    btree();
    ~btree();
    void destroy_tree(node *leaf);
    void insert(int key, node *leaf);
    node *search(int key, node *leaf);
    public:
    void insert(int key);
    node *search(int key);
    void destroy_tree();
    };

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

    btree::~btree()
    {
    destroy_tree();
    }

    void 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);
    }

    void main()
    {
    cout << "yeah"
    }

    and here's the error:

    --------------------Configuration: search a tree - Win32 Debug--------------------
    Compiling...
    source.cpp
    Linking...
    source.obj : error LNK2001: unresolved external symbol "private: void __thiscall btree::destroy_tree(struct node *)" (?destroy_tree@btree@@AAEXPAUnode@@@Z)
    Debug/search a tree.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    search a tree.exe - 2 error(s), 0 warning(s)

    thanks.

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    unresolved external errord are fun...... b/c they don't tell you where the problem is generated,
    Your Linker flags an error if your source code cannot be linked (attached) together, a lot of times it happens if you have few separete source files and start including one in the other....

  3. #3
    Unregistered
    Guest
    There aren't any other files.

  4. #4
    Unregistered
    Guest
    I just noticed an error, and it's not the one causing it, but it's the cout line in the main function. There isn't a ; at the end.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    Code:
    void destroy_tree(node *leaf) 
    { 
    if(leaf!=NULL) 
    { 
    destroy_tree(leaf->left); 
    destroy_tree(leaf->right); 
    delete leaf; 
    } 
    }
    I am not a big fan of classes, so I am not sure about this one, but don't functions that are declared within a class scope need to be defined with one? Also, please use code tags
    I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.

    Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.

  6. #6
    Unregistered
    Guest
    <code>
    #include <iostream.h>

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

    class btree
    {
    node *root;
    btree();
    ~btree();
    void destroy_tree(node *leaf);
    void insert(int key, node *leaf);
    node *search(int key, node *leaf);
    public:
    void insert(int key);
    node *search(int key);
    void destroy_tree();
    };

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

    btree::~btree()
    {
    destroy_tree();
    }

    void 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);
    }

    void main()
    {

    }
    </code>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21