再说一次,我正在研究zipType
's 数组的 qsort。我写的比较函数是这样的:
int compare(const void *v1, const void *v2){
const struct zipType *p1 = v1;
const struct zipType *p2 = v2;
if (p1->postalCode > p2->postalCode)
return(+1);
else if (p1->postalCode < p2->postalCode)
return(-1);
else
return(0);
}
这是使用它的函数:
void binRead(zipType *zip, fstream *input){
int junk;
int count;
int end;
input->seekg(0,ios::end);
end=input->tellg();
count=input->tellg()/24;
input->seekg(0);
while(input->tellg()!=end){
for(int i=0;i<count;i++){
input->read((char*)( &(zip[i].postalCode) ), sizeof(int ));
input->read((char*)( &junk ), sizeof(int ));
input->read((char*)( &(zip[i].longitude) ), sizeof(double ));
input->read((char*)( &(zip[i].latitude) ), sizeof(double ));
cout << "Currently at position" << input->tellg() << endl;
}
}
cout << "Array Created, Please wait while I sort" << endl;
qsort(zip, count, sizeof(int), compare);
usleep(3000000);
cout << "Array Sorted" << endl;
}
我得到的错误是其中几个:
invalid conversion from ‘const void*’ to ‘const zipType*’ [-fpermissive]
const struct zipType *p2 = v2;
其中之一:
error: cannot convert ‘zipType’ to ‘void*’ for argument ‘1’ to ‘void qsort(void*, size_t, size_t, int (*)(const void*, const void*))’
qsort(*zip, count, sizeof(int), compare);
任何想法我应该做什么?