Domanda

Me again, I'm working on the qsort of the array of zipType's. The compare function I wrote looks like this:

    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);
        }

This is the function that uses it:

   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;

  }

The error I'm getting is a couple of these:

    invalid conversion from ‘const void*’ to ‘const zipType*’ [-fpermissive]
    const struct zipType *p2 = v2;

And one of these:

     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);

Any ideas what I should be doing?

È stato utile?

Soluzione 2

You should read qsort firstly.Then you will know the meaning of each parameter.

Your qsort should be like : qsort((void*)zip, count, sizeof(struct zipType), compare);

The third parameter size means the size of the elem in the array, should be zipType not int.

Altri suggerimenti

To fix the first error, you need to cast from void* inside the comparison routine:

auto p1 = static_cast<const struct zipType*>(v1);

To fix the second error, I think we again need to cast, but this time to void*:

qsort(static_cast<void*>(zip), count, sizeof(zipType*), compare)

Honestly, I'm not sure why this cast is needed. If I'm remembering my conversion rules correctly, zipType* should be convertible to void* implicitly. If anyone knows, post a comment and I'll edit the answer or, if you can, just edit the answer.

Note that you want the size of the the elements in the array for the third parameter, not sizeof(int) here. You have an array of zipType* not int.

Since you are using C++ (deduced because you are using std::cout), though, try std::sort. It is more type-safe and can often be optimized better.

std::sort(zip, std::advance(zip, count), compare)

Also, since this is C++, you don't need to say struct zipType. You can just say zipType.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top