qsort function with predefined arrays

Hi all, hope someone can help me with this..

I have checked: http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/

And now trying it with my class... I have no clue how to fix this in order to use qsort

My array is:

Right now is not working so i don't care about the return..

1
2
3
4
5
6
7
8
int compare(const void *a, const void *b ){
	return 1;
}

....
CArray <Info,Info&> ArrayInfo;

qsort(ArrayInfo, ArrayInfo.GetSize(),sizeof(CArray <Info, Info&>), compare);


cannot convert parameter 1 from 'class CArray<class Info,class Info &>' to 'void *' this is the main error

Someone knows how to fix it?

Thanks for the help,

Regards
Last edited on
It needs to be a pointer to the first element of the array. It will only work for arrays that are stored contiguously in memory.
Last edited on
Thanks for the reply

Well if i use pointers it works...

but as my arrayinfo is not a pointer I need it to work that way. Is it possible?

so what I don't really understand is... the example in cplusplus.com uses a static array and works.

1
2
3
int values[] = { 40, 10, 100, 90, 20, 25 };
....
qsort (values, 6, sizeof(int), compare);


My code:

1
2
3
4
.h
static int compare(const void *a, const void *b)
CArray <Info, Info&> ArrayInfo;
...


1
2
3
4
5
6
7
8
9
10
11
.cpp

...

int CInfo::compare(const void *a, const void *b ){
	return 1;
}

bool CInfo::Sort(){	
       qsort(ArrayInfo, ArrayInfo.GetSize(),sizeof(CArray <Info, Info&>), CInfo::compare);
}


The error is still the same: cannot convert parameter 1 from 'class CArray<class Info,class Info &>' to 'void *' this is the main error

So any solution possible? or unless is a pointer i won't be able to use qsort in this case?

Thanks again,

Regards
Last edited on
Solved

1
2
3
4
5
6
7
int CInfo::compare(const void *a, const void *b ){
	return 1;
}

bool CInfo::Sort(){	
       qsort(ArrayInfo, ArrayInfo.GetSize(),sizeof(CArray <Info, Info&>), CInfo::compare);
}



1
2
3
bool CInfo::Sort(){	
qsort((void*)&(ArrayInfo.ElementAt(0)), ArrayInfo.GetSize(),sizeof(ArrayInfo.ElementAt(0)), CInfo::compare);
}
Last edited on
Topic archived. No new replies allowed.