Re: qsort in struc //HELP NEEDED ASAP :-S

Posted 24 March 2012 - 03:20 PM

If you can't include anything else then no you can't use std::sort. Post the code where you tried to sort your array. Also post the complete error messages exactly as they appear in your development environment.

Re: qsort in struc //HELP NEEDED ASAP :-S

Posted 24 March 2012 - 03:31 PM

Are you sure that you've exhausted all possible resources which might help you implement quick sort? Googling "qsort tutorial", I found this tutorial posted on dreamincode.net which may help you. There's so many tutorials, code exampled and more out there though, surely you could come up with something.

Re: qsort in struc //HELP NEEDED ASAP :-S

Posted 24 March 2012 - 03:57 PM

It's rather simple.

qsort is a "generic" function, to sort any kind of data, so it takes as an argument a function capable of comparing two generic datatypes, which takes two const void * arguments. The use of const void * arguments allows any type of data to be passed to the function.

Within the function you "unpack" the passed data into their actual types for comparison. For example, if you wanted to compare two ints, it would look like this:

Re: qsort in struc //HELP NEEDED ASAP :-S

Posted 24 March 2012 - 04:06 PM

As I've already said - I tried two days to find out, how to implement it, but I wasn't able to fix the errors :-/ I think, that the main problem is not, that I would not understand, how it works, but the main problem is a poor level of my C++ skills... As a longtime java and C# programmer, who just started with C++, I'm really, really confused with this language. :-S

Re: qsort in struc //HELP NEEDED ASAP :-S

Posted 24 March 2012 - 04:35 PM

Which is complicated by the fact that struct item is nested within the class. Which means the comparator function needs to be a member (which you actually declared it as), and in addition it must be static, and thus passed to qsort as &CPhoneBook::struct_cmp_by_name

EDIT: And, because itemArr is a pointer, you cannot use sizeof on it; you need to use the itemCount variable.

Re: qsort in struc //HELP NEEDED ASAP :-S

Posted 24 March 2012 - 04:55 PM

JackOfAllTrades, on 25 March 2012 - 01:35 AM, said:

Which is complicated by the fact that struct item is nested within the class. Which means the comparator function needs to be a member (which you actually declared it as), and in addition it must be static, and thus passed to qsort as &CPhoneBook::struct_cmp_by_name

Re: qsort in struc //HELP NEEDED ASAP :-S

My post #10 was wrong because I was thinking C when I wrote it -- I wonder why -- maybe because I was looking at "qsort" and "strcmp" at the time... I edited it so it's sane now.

The comparator function can't be a member function of the class because then it won't satisfy the required signature

int (*)(void *, void *)

but it doesn't have to be a friend since item is a public member of CPhoneBook, and item is a struct so all of its members are public as well. But the declarations of the local variables in the comparator need a scope qualifier.

The only other error in struct_cmp_by_name was the attempted use of strcmp to compare two std::strings -- obviously that can't work.

So, take the declaration of struct_cmp_by_name out of the CPhoneBook class definition and place it before the class definition.

Then take this

int struct_cmp_by_name(const void *a, const void *b)
{
// the following are wrong because they are trying to declare a new type "struct itemArr"
// struct itemArr *ia = (struct itemArr *)a;
// struct itemArr *ib = (struct itemArr*)b;
// You need to cast the void* to an item*, but item is defined inside CPhoneBook, so...
CPhoneBook::item *ia = (CPhoneBook::item*)a;
CPhoneBook::item *ib = (CPhoneBook::item*)b;
// The following is wrong because name is a std::string, not a c_string
return strcmp(ia->name, ib->name);
}

Fix the last line of that function and you will be "good to go".

Oh, you'll also have to fix the qsort call inside your sort() function which is about 3/4 wrong -- I'll let you ponder that one: read the compiler error message and qsort documentation.

This post has been edited by r.stiltskin: 24 March 2012 - 08:12 PM
Reason for edit:: I keep forgetting to turn off the damn emoticons