Thread: C++ link error with qsort

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    C++ link error with qsort

    dear all,

    I'm having a link error with MSVC++.
    I have been using qsort successfully with a C program.
    I'm now introducing some C++ code into this program as I need to use some other C++ routines with it - so I've changed the suffix to .cpp not .c
    unfortunately MSVC++ now gives me a link error with I think is related to my use of qsort - C++ is more strict with qsort arguments than C ?
    the error is
    myprog.obj : error LNK2001: unresolved external symbol "int __cdecl comprows2(void const *,void const *)" (?comprows2@@YAHPBX0@Z)
    Debug/myprog.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    where comprows2 is my qsort compare function
    many thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Paste some code - in particular your compare function?

    Does it look like the compare function prototype in the error message?

    Note that
    Code:
    int __cdecl comprows2(void const *a ,void const * b) {
    }
    is probably different to
    Code:
    int comprows2(void const * a ,void const *b) {
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    dear Salem

    many thanks for your help -
    I have also just tried to include __cdecl in my function prototype as
    Code:
    int __cdecl comprows2(void *x1,void *x2)
    but that gives the same error

    the compare function code is

    Code:
    int comprows2(void *x1,void *x2)
    {
    int *r1,*r2;
    int i,ret;
    
    r1=*(int **)x1;
    r2=*(int **)x2;
    if (r1[dis[0]]<r2[dis[0]]) return -1;
    else if (r1[dis[0]]>r2[dis[0]]) return 1;
    else
       {
       if (r1[dis[1]]<r2[dis[1]]) return -1;
       else if (r1[dis[1]]>r2[dis[1]]) return 1;
       else 
    	  {
    	  ret=0;
    		for(i=0;i<nmark;i++) 
    		{
    		if (r1[marker[i]]<r2[marker[i]]) 
    			{
    			ret= -1;
    			break;
    			}
    		else if (r1[marker[i]]>r2[marker[i]]) 
    			{
    			ret= 1;
    			break;
    			}
    		}
    	  return ret;
    	  }
    		
       }
    }
    and I call it with qsort as
    Code:
     qsort(haplo0,nchrom,sizeof(int *),comprows2);
    haplo0 is a 2D int array and dis is a 1D array containing the columns I want to sort by

    many thanks again

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    I ain't sure, but should this:

    Code:
    qsort(haplo0,nchrom,sizeof(int *),comprows2);
    Actually be

    Code:
    qsort(haplo0,nchrom,sizeof(int *),comprows2() );
    Since comprows2 is a function.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    >qsort(haplo0,nchrom,sizeof(int *),comprows2() );
    Absolutely not!
    This would call the function, not pass a pointer to the function (which is what we want to do)

    @bvnorth
    Can you strip this down to something which is complete and compiles - a small main() and all the include files you're including?

    > int comprows2(void *x1,void *x2)
    On the other hand, perhaps you should make those two parameters 'const' like it says in the error message.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    many thanks Salem and Sfin,

    in the meantime I did actually try a few variants of "const void" and "void const" in my prototypes (without success) but I may have a more serious go 2moro since you think that may be the problem.
    I've left the office and my program behind for the dat now but I'll follow your advice tomorrow.
    many thanks

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    goodness
    including const in the prototype does work !!!
    I did think I'd tried that before !!
    so the take home message for me is that
    Code:
    int comprows2(void const *x1,void const *x2)
    is the correct way to define a compare function for qsort in C++

    many thanks Salem and Sfin

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    It's also the correct way in C, it's just that the C++ compiler is a lot more strict about such things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List, Please Help!
    By CodeMonkeyZ in forum C Programming
    Replies: 5
    Last Post: 02-17-2009, 06:23 AM
  2. I'm confused about link lists (again)
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 08:13 PM
  3. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Undefined Structure in Link List
    By _Cl0wn_ in forum C Programming
    Replies: 1
    Last Post: 03-22-2003, 05:57 PM