0

I'm trying to use qsort in a member function, but I get the following error

I don't know what's wrong with the following code. so I tried to put the address of operator but still it didn't work.

 error C2664: 'void qsort(void *,std::size_t,std::size_t,int (__cdecl *)(const void *,const void *))': cannot convert argument 4 from 'int (__thiscall Rasterizer::* )(facet **,facet **)' to 'int (__cdecl *)(const void *,const void *)'

here are the two functions:

  int Rasterizer:: Poly_Compare(facet_ptr arg1, facet_ptr arg2)
{
    // this function comapares the average z's of two polygons and is used by the
    // depth sort surface ordering algorithm

    float z1, z2;


    // dereference the poly pointers



    // compute z average of each polygon

    if (arg1->num_points == 3)
    {
        // compute average of 3 point polygon

        z1 = (float)0.33333*(arg1->vertex_list[0].z +
            arg1->vertex_list[1].z +
            arg1->vertex_list[2].z);
    }
    else
    {
        // compute average of 4 point polygon

        z1 = (float)0.25*(arg1->vertex_list[0].z +
            arg1->vertex_list[1].z +
            arg1->vertex_list[2].z +
            arg1->vertex_list[3].z);

    } // end else




      // now polygon 2

    if (arg2->num_points == 3)
    {
        // compute average of 3 point polygon

        z2 = (float)0.33333*(arg2->vertex_list[0].z +
            arg2->vertex_list[1].z +
            arg2->vertex_list[2].z);
    }
    else
    {
        // compute average of 4 point polygon

        z2 = (float)0.25*(arg2->vertex_list[0].z +
            arg2->vertex_list[1].z +
            arg2->vertex_list[2].z +
            arg2->vertex_list[3].z);

    } // end else

      // compare z1 and z2, such that polys' will be sorted in descending Z order

    if (z1 > z2)
        return(-1);
    else
        if (z1 < z2)
            return(1);
        else
            return(0);

} // end Poly_Compare


  //////////////////////////////////////////////////////////////////////////////

void Rasterizer::Sort_Poly_List(void)
{
    // this function does a simple z sort on the poly list to order surfaces
    // the list is sorted in descending order, i.e. farther polygons first

    std::sort(world_polys, num_polys_frame + world_polys, Rasterizer::Poly_Compare);

} 

typedef struct facet_typ
{

    int num_points;  // number of vertices

    int color;       // color of polygon

    int shade;       // the final shade of color after lighting

    int shading;     // type of shading to use

    int two_sided;   // is the facet two sided

    int visible;     // is the facet transparent

    int clipped;     // has this poly been clipped

    int active;      // used to turn faces on and off

    point_3d vertex_list[MAX_POINTS_PER_POLY]; // the points that make
    // up the polygon facet

    float normal_length;  // holds pre-computed length of normal

} facet, *facet_ptr;
احمد
  • 113
  • 1
  • 2
  • 7
  • 1
    Even if the other arguments could safely be cast to `const void *`, a member function is not a normal function: it has a `this` pointer to account for. Either wrap your function (cfr. `bind`) or make it static – Marco A. Oct 24 '16 at 21:43
  • 2
    Don't use `qsort` in C++. It's a legacy C function with a broken interface. Use `std::sort`. – n. m. could be an AI Oct 24 '16 at 21:46
  • @MarcoA. I made it a static, but I get that error static' should not be used on member functions defined at file scope – احمد Oct 24 '16 at 21:47
  • @احمد If you have a new question, please post it as a new question. If the question is about non-working code, please make sure to include a [mcve]. – Baum mit Augen Oct 24 '16 at 21:49
  • 1
    Stop this and use `std::sort`. The `qsort` is not only a broken interface, it will tear your object to shreds if `world_polys` is non-POD. – PaulMcKenzie Oct 24 '16 at 21:50
  • [Don't put the `static` keyword for the member function definition.](http://stackoverflow.com/q/25977268/1460794) – wally Oct 24 '16 at 21:51
  • @احمد Please post what `world_polys` is. If it's not a POD type, you can throw this entire code away and basically this entire question is null and void, as the behavior is undefined when using `qsort` on such types. – PaulMcKenzie Oct 24 '16 at 21:59
  • @PaulMcKenzie facet_ptr world_polys[MAX_POLYS_PER_FRAME]; // the visible polygons for this frame – احمد Oct 24 '16 at 22:00
  • @PaulMcKenzie I have updated the post – احمد Oct 24 '16 at 22:04
  • @n.m. can you rewrite it using std::sort ? I'm confused on how to port the code to std::sort – احمد Oct 24 '16 at 22:06
  • @احمد -- `std::sort(world_polys, world_polys+num_polys_frame,Rasterizer::PolyCompare);`. Then `PolyCompare` is simply `PolyCompare(facet* arg1, facet* arg2)` { }` where you return `true` if arg1 comes before arg2, `false` otherwise. None of this double pointer, void*, casting all over the place, monstrosity. – PaulMcKenzie Oct 24 '16 at 22:12
  • @PaulMcKenzie std::sort(world_polys, num_polys_frame + world_polys, Rasterizer::Poly_Compare); I have used the following code, but still complains 'Rasterizer::Poly_Compare': non-standard syntax; use '&' to create a pointer to member – احمد Oct 24 '16 at 22:15
  • @PaulMcKenzie I have posted an updated code using std::sort, but It doesn't work – احمد Oct 24 '16 at 22:24
  • @احمد Please [see this](http://ideone.com/fTjTgZ). And to be honest, even that code can be simplified by using container classes instead of raw pointers. You're supposed to return `true` or `false` from the comparison function, not 1, 0, -1. If arg1 comes before arg2 in the sort, you return true, else you return false. – PaulMcKenzie Oct 24 '16 at 22:27
  • @PaulMcKenzie Thanks a lot it worked. but how would I access world_polys[curr_poly]->vertex_list[0].z; ?? and why its a pointer to pointer ? why not just a pointer to facet ? – احمد Oct 24 '16 at 22:32
  • @PaulMcKenzie now I have that error error C2664: 'int (facet *,facet *)': cannot convert argument 2 from 'facet **' to 'facet *' – احمد Oct 24 '16 at 22:41
  • Please [see this](http://ideone.com/yXVw7j). You have to realize that `std::sort`'s arguments are references to the types that are stored within your array. – PaulMcKenzie Oct 25 '16 at 00:40

0 Answers0