G
gallows
I've tried to use C qsort() on an object derivate by std::vector, but
it doesn't work.
I've the follow structure:
struct Item {
std::string name;
int number;
}
And the class "Container":
class Container : public std::vector<Item> { /* ... */ }
sort method is the follow:
void Container::sort()
{
qsort(this, this->size(), sizeof(Item), compare_items_by_name);
}
compare function is:
int compare_items_by_name(const void* p, const void* q)
{
return static_cast<const
Item*>(p)->name.compare(static_cast<const Item*>(q)->name);
}
My compiler (g++ 4.1) doesn't give warnings nor errors, but:
Program received signal SIGSEGV, Segmentation fault.
0xb7ee700e in std::string::compare () from /usr/lib/libstdc++.so.6
I can't find the mistake, maybe in the casting?
Thanks a lot!
s.
it doesn't work.
I've the follow structure:
struct Item {
std::string name;
int number;
}
And the class "Container":
class Container : public std::vector<Item> { /* ... */ }
sort method is the follow:
void Container::sort()
{
qsort(this, this->size(), sizeof(Item), compare_items_by_name);
}
compare function is:
int compare_items_by_name(const void* p, const void* q)
{
return static_cast<const
Item*>(p)->name.compare(static_cast<const Item*>(q)->name);
}
My compiler (g++ 4.1) doesn't give warnings nor errors, but:
Program received signal SIGSEGV, Segmentation fault.
0xb7ee700e in std::string::compare () from /usr/lib/libstdc++.so.6
I can't find the mistake, maybe in the casting?
Thanks a lot!
s.