Thread: some problems my code

  1. #1
    Registered User
    Join Date
    Apr 2014
    Location
    Gebze
    Posts
    6

    some problems my code

    İ wrote bubble sort function,i want to run this function each type and class for example class Collos.but it does not compile.i wrote comparator each type and class but not worked. where is the problem.
    Code:
    class Collos{
    	public:
    		int collo;
    		int wottlo;
    };
    
    
    
    void sort(void *newarray,size_t total_elems ,int (* comparator) (const void *, const void *) ){		
    	void *temp;
     	
    	for ( int i = 0 ; i < total_elems-1 ; ++i){
    		for ( int j = 0; j <total_elems-i-1 ; ++j ){
    			
    			if( (*comparator)( *(void *)newarray[j],*(void*)newarray[j+1] )){
    				
    				 *temp = *newarray[j];
    				 *newarray[j] = *newarray[j+1];
    				 *newarray[j+1] = * temp;
    				 
    			 }
    			 
    		}
    	}
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The compiler error is because it is not possible to dereference a void pointer (and your code tries to do that in multiple ways). It is necessary to know the type of whatever the void pointer actually points at.

    Even without that, your logic for sorting is broken anyway.

    Why not use std::sort()?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    4
    I don't think you want to have "i < total_elems -1;"

    But I'm with grumpy that you shouldn't be rolling your own sort.

  4. #4
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    I don't think you want to have "weeks_since_this_thread_was_active > 4"

    But I'm with the general rules of all forums that you shouldn't be gravedigging 'not' your own thread.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Code! Having problems.
    By Invertalon in forum C Programming
    Replies: 4
    Last Post: 03-05-2008, 06:17 AM
  2. Code problems.....once again
    By NicAuf in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2007, 02:55 PM
  3. Problems with code
    By hollyjaye in forum C Programming
    Replies: 11
    Last Post: 03-29-2007, 05:42 AM
  4. Problems with code
    By Alastor in forum C Programming
    Replies: 9
    Last Post: 10-04-2005, 06:44 PM
  5. Problems with code from the FAQ
    By Bhaunted in forum C++ Programming
    Replies: 1
    Last Post: 08-31-2004, 02:36 PM