Problem with comparator function for qsort on struct array

Hi, I've got a problem with calling the qsort function on a struct array. The data structure is the following:


1
2
3
4
5
struct student{
    int grade;
    int studentID;
    string name;
};


This is the comparator function I came up with:

1
2
3
4
5
6
7
8
9
int compare(void *p1, void *p2)
{
    int grade1 = ((struct student*)p1)->grade;
    int grade2 = ((struct student*)p2)->grade;
    
    
    return grade1 - grade2;

}


This is the program, in which I tried to call qsort:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Header.h"


int main()
{
    
    student students[] = {
        {87, 10001, "Fred"},
        {28, 10002, "Tom"},
        {100, 10003, "Alistair"},
        {78, 10004, "Sasha"},
    };
    qsort( students, 4, sizeof(struct student), compare);
    
    for ( int i = 0; i < 4; i++)
        cout << students[i].grade << students[i].studentID << students[i].name << endl;
        
        
    
    return 0;
}


The error that I get is "No matching function to call for qsort", so I guess it's a problem of the comparator function. Does anybody know why I get this error and what is wrong with my comparator function?
qsort() cannot be used with struct student because it is not trivally-copyable. Why even consider it at all when C++ has the much faster std::sort? (which also can be used with your struct student)

In any case, if you aren't missing #include <cstdlib> , your comparison function needs to take const void*, not void* arguments in order for this to compile.
Last edited on
Thanks a ton for your answer, it worked when using const void*.

The reason I used qsort was because it was asked for in the book ( "Think like a programmer" by Anton Spraul) I'm reading. I didn't know about the sort function yet, thanks for the suggestion.
closed account (3qX21hU5)
For more information on std::sort() you can check out the tutorial here http://www.cplusplus.com/articles/NhA0RXSz/ which isn't the most in depth one (IE it doesn't go into detail on lambda's, functors and stuff like that but should give you a good grounding to start off with).

I would also highly suggest looking into a better book if your C++ book is suggesting you use qsort() specially when it is a C++ book. Also since it looks like it is teaching C and not C++. Though that is only a suggestion and is quite biased I will admit since I am not really for the whole learn C to learn C++ attitude.
Last edited on
wow that book seriously says "The default fast sort for C/C++ programmers is the qsort function in the standard library"
And it seriously sends structs with std::string members into qsort().
Get rid of it before it's too late.
Last edited on
There's a thread about the book in the lounge, and even a post from the author.
http://www.cplusplus.com/forum/lounge/82029/
Well I'm using the book more for the problem solving techniques it offers rather than to learn C++ (I'm using "Accelerated C++" for that purpose) and I think it does a fairly good job at that.
Last edited on
Topic archived. No new replies allowed.