Thread: qsort Comparator

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    qsort Comparator

    Hi, i would like to sort an array of object by their atribute, this is my testing project, the biggest problem is in compare function

    Thanks for help


    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <algorithm>
    #include "string.h"
    #include <cstdlib>
    
    
    using namespace std;
    using std::sort;
    
    class A{
    public:
         int size;
    
    
    public :
         int  get(){
              return  size;
         }
    };
    
    int compare (const void *a, const void *b) {
        A *aO = *(A **)a;
        A *bO = *(A **)b;
        return ( aO.get() - bO.get());   // now i gets error request for member of get in aO which  is none-class A*  dunno why
    }
    int main()
    {
        A **aray;
        array= new A *[4];
    
    
        for(int i=0; i < 4; i++)
        {
             array[i] = NULL;
        }
    
    
        A* a= new A();
        A* b= new A();
        A* c= new A();
        A* d= new A();
        a->size=15;
    
    
        array[0]= b;
        array[1]= a;
        array[2]= c;
        array[3]= d;
        array[0]->size=12;
        array[1]->size=10;
        array[2]->size=20;
        array[3]->size=15;
    
    
        qsort (pole[0],4,sizeof(A*),compare);
    
    
        for(int i=0; i < 3; i++)
        {
           if(array[i]!= NULL)
            cout << array[i]->get()  << endl;
        }
        return 0;
    }
    Last edited by flegmik; 10-19-2012 at 04:23 PM. Reason: some flows and mistakes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort help
    By bbray in forum C Programming
    Replies: 11
    Last Post: 09-18-2011, 01:25 PM
  2. qsort example
    By blob84 in forum C Programming
    Replies: 7
    Last Post: 10-14-2010, 03:36 AM
  3. qsort
    By dayknight in forum C++ Programming
    Replies: 9
    Last Post: 09-17-2004, 02:06 PM
  4. qsort please help. Thanks
    By Ann Lim in forum C Programming
    Replies: 8
    Last Post: 12-12-2002, 02:20 AM
  5. qsort
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-30-2001, 05:35 PM