QSORT --and undefined external

This is a discussion on QSORT --and undefined external within the C++ Programming forums, part of the General Programming Boards category; hi, Im starting to read up on arrays.I copied some code and when i try to compile it I keep ...

  1. #1
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26

    QSORT --and undefined external

    hi,
    Im starting to read up on arrays.I copied some code and when i try to compile it I keep getting:

    C:\Users\ghetto smurf\Documents\Visual Studio 2008\Projects\Array\Debug\Array.exe : fatal error LNK1120: 1 unresolved externals

    here is the code

    Code:
    #include<iostream>
     
    
    using namespace std;
    
    //function definitions///////////////////////////////
    
    int sortArray(const void *a,const void *b)
    {
    	int objA=*((int*)a);
    	int objB=*((int*)b);
    	if (objA<objB)
    		return -1;
    	if (objA==objB)
    		return 0;
    }
    /////////////////////////////////////////////////////
    int main()
    {
    	int someArray[]={12,23,31,4,5,16};
    	int postion;
    
    	qsort((void*)someArray,6,sizeof(someArray[0]),sortArray);
    
    cin.get();
    return 0;
    }
    I have a feeling im missing something obvious.Have i not included the libary that qsort is a member of?

  2. #2
    Registered User
    Join Date
    Dec 2007
    Location
    Denmark
    Posts
    14
    I'm pretty sure you're missing:

    Code:
     #include <stdlib.h>

  3. #3
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26

    Unhappy

    Tried that and still showing the same error message.I have Double checked the code and its copied verbatim as far as i can tell..

  4. #4
    Registered User
    Join Date
    Dec 2007
    Location
    Denmark
    Posts
    14
    It works fine with me...

    Code:
    #include<iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    //function definitions///////////////////////////////
    
    int sortArray(const void *a,const void *b)
    {
    	int objA=*((int*)a);
    	int objB=*((int*)b);
    	if (objA<objB)
    		return -1;
    	if (objA==objB)
    		return 0;
    }
    /////////////////////////////////////////////////////
    int main()
    {
    	int someArray[]={12,23,31,4,5,16};
    	int postion;
    
    	qsort((void*)someArray,6,sizeof(someArray[0]),sortArray);
            for(int i = 0;i<6;i++){
                cout << someArray[i] << endl;
            }
        cin.get();
        return 0;
    }
    However, I don't think the sortArray function works correctly. I don't know what you want it to do though..

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, it compiles and links fine here. Does it perhaps mention, what the unresolved external is?

    As to your comparison function, you are not returning anything if objA > objB.

    And in C++ you can save yourself some trouble and use C++'s sort function:
    Code:
    #include <algorithm>
    
    int main()
    {
        int someArray[]={12,23,31,4,5,16};
        std::sort(someArray, someArray + 6);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    thx will try that ...seems alot less complex.No it doesnt mention what the external that is undefined(i think it normally does with varibles).

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by Labmouse View Post
    thx will try that ...seems alot less complex.No it doesnt mention what the external that is undefined(i think it normally does with varibles).
    I'm pretty sure it does. You need to look in the output window and scroll up.
    Well at least we can see that it's VS2008, and that you're running on Vista, and even that your user name is 'ghetto smurf' . Use what anon shows.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    thx all....and ghetto smurf is my real name

Popular pages Recent additions subscribe to a feed

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