Thread: need help undefined reference to insert

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    3

    need help undefined reference to insert

    hello when i try to run this code i'm gettin an error
    "undefined reference to 'insert'

    pleaze someone help me


    this is the code

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    typedef struct pbentry {
        char nom[30];
        char prenom[30];
        int numero;
        float moyenne;
    } Entry;
    
    /*Create tree node structure.*/
    struct tree_node {
        Entry data;
        struct tree_node *left;
        struct tree_node *right;
    };
    struct tree_node * insert(struct tree_node *p, Entry e);
    struct tree_node * create_node (struct tree_node *q, struct tree_node *r, Entry e);
    
    
    int main(void)
    {
        int option = 0; /*Variable for option selection.*/
        Entry e;  /*Basic entry*/
        struct tree_node *p = NULL; /*Basic tree node*/
        char ln[16]; /*Used for deletions, editing, and searching*/
        char fn[11]; /*Used for deletions, editing, and searching*/
    
        /*Return to menu after each instruction until the user quits.*/
        while (option != 6) {
            /*Show user the option menu.*/
            printf("MENU\n");
            printf("1. Add\n");
            printf("2. Delete\n");
            printf("3. Edit\n");
            printf("4. Search\n");
            printf("5. List\n");
            printf("6. Quit\n");
            /*Get option from the user.*/
            printf("\nPlease select an option: ");
            scanf("%d", &option);
            /*If option is 1 (Add):*/
            if (option == 1) {
                /*Take in subject data from the user.*/
                printf("Ajouter le n: ");
                scanf("%s", &e.numero);
                printf("Ajouter le nom ");
                scanf("%s", &e.nom);
                printf("Ajouter le prenom ");
                scanf("%s", &e.prenom);
                printf("Ajouter la moyenne ");
                scanf("%s", &e.moyenne);
                /*Create a new node.*/
                p = insert(p, e);
                /*Confirm node creation.*/
                printf("Record added successfully.\n\n");
            }
    
    
    /*Adds a node to the tree.*/
    struct tree_node * insert(struct tree_node *p, Entry e){
    
        if (p == NULL) {
    
            p = create_node(NULL, NULL, e);
        }
    
        else if (e.numero < p->data.numero) {
    
            p->left = insert(p->left, e);
        }
    
        else if (e.numero > p->data.numero)  {
    
            p->right = insert(p->right, e);
        }
    
            else {
    
                return p;
            }
        }
    
        return p;
    }
    
    /*Creates a new node.*/
    struct tree_node * create_node (struct tree_node *q, struct tree_node *r, Entry e) {
        struct tree_node* newnode;
        newnode = (struct tree_node*)(malloc(sizeof(struct tree_node)));
        newnode->data = e;
        newnode->left = q;
        newnode->right = r;
        return newnode;
    }
        }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You've defined insert and create_node inside main.
    In the above listing, you need to move the brace at line 96 to line 58.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    3
    Quote Originally Posted by oogabooga View Post
    You've defined insert and create_node inside main.
    In the above listing, you need to move the brace at line 96 to line 58.
    still get the same error

    undefined reference to 'insert'

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need yet another brace before the final brace of main.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    3
    got another error

    D:\test\main.c|97|error: expected identifier or '(' before '}' token|

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You have more problems than just that. You need to compile with warning level set to max (e.g. -Wall option for gcc)
    Code:
    $ make foo
    gcc -g -Wall -std=c99 -o foo foo.c
    foo.c: In function ‘main’:
    foo.c:46:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat]
    foo.c:48:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[30]’ [-Wformat]
    foo.c:50:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[30]’ [-Wformat]
    foo.c:52:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘float *’ [-Wformat]
    foo.c:27:10: warning: unused variable ‘fn’ [-Wunused-variable]
    foo.c:26:10: warning: unused variable ‘ln’ [-Wunused-variable]
    foo.c: At top level:
    foo.c:85:1: error: expected identifier or ‘(’ before ‘return’
    foo.c:86:1: error: expected identifier or ‘(’ before ‘}’ token
    foo.c: In function ‘insert’:
    foo.c:83:1: warning: control reaches end of non-void function [-Wreturn-type]
    make: *** [foo] Error 1
    All those "format '%s'" errors are because you're using either the wrong format specifier in scanf (%s is for string data only), or you're passing in the wrong type (e.g. if you want to read a string, then you need to provide a char *). Read the scanf documentation to learn the right types for the data you want. Note, I generally prefer reading a line with fgets and using sscanf (note the extra 's' to scan from a string) to parse out the data. It's better for handling input errors (users make typos). sscanf has the same format specifier rules as scanf, so the change is pretty easy.

    Unused variables should be removed.

    Note, all I did is move the one } curly bracket and add another to main. I don't know why our versions are 10 lines off, but if you have other code you added, you should have posted updated code so we can help you fix that code.

    You should get a decent editor with syntax highlighting, auto-indentation and brace matching (there are lots of free options). That will help you solve many of these problems. In the version of insert() I'm working with, your curly brackets don't quite match up. Who knows if your code has the same problems, since we're clearly using different code.
    Last edited by anduril462; 12-28-2013 at 03:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined reference
    By Trinity32244 in forum C Programming
    Replies: 14
    Last Post: 04-08-2011, 12:48 PM
  2. : undefined reference to `pow'
    By mohitsaxena019 in forum C Programming
    Replies: 13
    Last Post: 07-25-2010, 08:35 AM
  3. undefined reference to...
    By funkybomber in forum C++ Programming
    Replies: 1
    Last Post: 11-26-2006, 09:59 PM
  4. undefined reference
    By Axel in forum C Programming
    Replies: 16
    Last Post: 09-11-2005, 09:42 PM
  5. undefined reference
    By laasunde in forum C++ Programming
    Replies: 6
    Last Post: 10-23-2002, 11:44 AM