Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: C++ undefined reference to "funtion"

  1. #1
    Join Date
    Nov 2007
    Location
    México, D.F.
    Beans
    30
    Distro
    Ubuntu 11.04 Natty Narwhal

    Question C++ undefined reference to "funtion"

    Hi,

    Im trying to lern c++ on my own and y have a problem while trying to do a static library:

    main.cpp
    Code:
    #include <iostream>
    #include "../Library/lib.h"
    
    using namespace std;
    
    int main()
    {
        int *arr[5];
        *arr[0]=40;
        *arr[1]=2;
        *arr[2]=77;
        *arr[3]=16;
        *arr[4]=12071;
    
        QuickSort(arr, 0, 4);
    
        for (int i=0; i<5; i++)
        {
            cout << *arr[i];
        }
    }
    lib.h
    Code:
    #ifndef LIB_INCLUDED
    #define LIB_INCLUDED
    
    static void QuickSort(int *datos[], int min, int max);
    
    static int Encontrar_Pivote(int *datos[], int min, int max);
    
    static void Rotar(int *datos[], int primero, int segundo);
    
    
    #endif // LIB_INCLUDED
    lib.cpp
    Code:
    #include "lib.h"
    
    static void QuickSort(int *datos[], int min, int max)
    {
        int pivote;
    
        if ((max-min) > 0)
        {
            pivote = Encontrar_Pivote(datos, min, max);
            QuickSort(datos, min, pivote-1);
            QuickSort(datos, pivote+1, max);
        }
    }
    
    static int Encontrar_Pivote(int *datos[], int min, int max)
    {
        int pivote = *datos[min++];
    
        while (min<max)
        {
            if (*datos[min]<=pivote && *datos[max]>=pivote)
            {
                min++;
                max--;
            }
            else if (*datos[min]>=pivote && *datos[max]<=pivote)
            {
    
                Rotar(datos, min, max);
                min++;
                max--;
            }
            else if (*datos[min]<=pivote && *datos[max]<=pivote)
            {
                min++;
            }
            else if (*datos[min]>=pivote && *datos[max]>=pivote)
            {
                max--;
            }
        }
    
        if (min>max)
            Rotar(datos, max, pivote);
    
        else if (min<pivote)
            Rotar(datos, min, pivote);
    
        else if (min>pivote)
            Rotar(datos, min-1, pivote);
    
        return 0;
    }
    
    static void Rotar(int *datos[], int primero, int segundo)
    {
        int aux = *datos[primero];
        *datos[primero] = *datos[segundo];
        *datos[segundo] = aux;
    }
    When i try to build the main file i get this error:
    C++/Pruebas/main.cpp|15|undefined reference to `QuickSort(int**, int, int)'

  2. #2
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: C++ undefined reference to "funtion"

    you left out the most important piece ... how are you building it?

    sounds like you are simply compiling the main file and not the library file.
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  3. #3
    Join Date
    Nov 2007
    Location
    México, D.F.
    Beans
    30
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: C++ undefined reference to "funtion"

    Im using Code blocks IDE and I build the library before trying to compile the main file.

    Do i have to use a special command to build a library?

  4. #4
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: C++ undefined reference to "funtion"

    You must remove the word "static" from the definitions and declarations of your functions.

    "static" has the side effect of meaning it is only visible in the current module so the linker can't see it. Note: All functions always have static memory allocation in code space.
    Last edited by worksofcraft; January 23rd, 2011 at 09:21 AM.

  5. #5
    Join Date
    Nov 2007
    Location
    México, D.F.
    Beans
    30
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: C++ undefined reference to "funtion"

    I keep getting the same error

  6. #6
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: C++ undefined reference to "funtion"

    Quote Originally Posted by Roberto_Lapuente View Post
    I keep getting the same error
    I copied your files and removed the word "static" everywhere.

    Then it compiles OK from the command line... but there is still a run time bug to fix:
    Code:
    cjs@cjs-ubuntu:~/Desktop/examples/Lapuente$ g++ main.cpp lib.cpp
    cjs@cjs-ubuntu:~/Desktop/examples/Lapuente$ ./a.out
    Segmentation fault
    cjs@cjs-ubuntu:~/Desktop/examples/Lapuente$
    would you like me to find why it seg' faults or are you still stuck on compiling it?

  7. #7
    Join Date
    Nov 2007
    Location
    México, D.F.
    Beans
    30
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: C++ undefined reference to "funtion"

    thanks, I will try to compile it from the command line.

  8. #8
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: C++ undefined reference to "funtion"

    The reason you get segmentation fault is because you create an ARRAY of POINTERS to integers. and you don't initialize the pointers.

    I suspect what you really want is an array of integers:
    PHP Code:
     int main()
    {
        
    int arr[5];
        
    arr[0]=40;
        
    arr[1]=2;
        
    arr[2]=77;
        
    arr[3]=16;
        
    arr[4]=12071;

        
    QuickSort(arr04);

        for (
    int i=0i<5i++)
        {
            
    cout << arr[i] << endl;
        }

    Edit: you also need to change your functions:
    PHP Code:
    void QuickSort(int *datosint minint max);

    int Encontrar_Pivote(int *datosint minint max);

    void Rotar(int *datosint primeroint segundo); 
    Note: in C and C++ pointers are used as above to pass arrays by reference.
    Last edited by worksofcraft; January 23rd, 2011 at 09:56 AM.

  9. #9
    Join Date
    Nov 2007
    Location
    México, D.F.
    Beans
    30
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: C++ undefined reference to "funtion"

    Thanks, i want an array if integers but i dont know how to pass an array by reference in c so i used the pointers.

  10. #10
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: C++ undefined reference to "funtion"

    Quote Originally Posted by Roberto_Lapuente View Post
    Thanks, i want an array if integers but i dont know how to pass an array by reference in c so i used the pointers.
    Uh... Is this C or C++?

    In C++, there's something called "references" and that works more or less as a call-by-reference system. In C, you only can do this by using pointers.

    But, in both languages arrays are always passed by pointers, so...

    Code:
    // I assume you're using g++
    #include <cstdio>
    
    void oh_noes(int *array, unsigned int size)
    {
        unsigned int i;
        for(i = 0; i < size; i++)
            (void)printf("%d\n", array[i]);
    }
    
    int main(void)
    {
        int array[6] = {1, 2, 3, 4, 5, 6};
    
        oh_noes(array, 6);
    
        return 0;
    }
    Why? Well, because arrays fall back to pointers. An array is nothing more than a pointer to the first element in some allocated region. The next elements are accessed by using pointer arithmetic, such that array[n] is actually *(array + n). The difference between arrays and pointers is that only the first allocate the needed space to place elements in it, while pointers don't.

    C++ references don't work well with this because arrays already are called by "reference" (actually, by pointer)... If you're curious about them, take a look at this: http://cplus.about.com/od/learning1/ss/references.htm
    Last edited by nvteighen; January 23rd, 2011 at 11:00 AM.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •