Thread: qsort function + template

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    qsort function + template

    I have to create a function template that will sort an int, float, or string filled array in ascending order. The function will receive an unordered array, and it will return an ordered array. Right now i am just trying to get the function to sort the int array. I am trying to use the qsort function, but i am having trouble. It comes up with the following error:
    'compare': local function definitions are illegal.
    my code:
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    template<typename Type>
    void Sort_Array(Type *Array1, int Num_Elem)
    {
    	Num_Elem = Num_Elem - 2;
    	while(Num_Elem >= 0)
    	{
    		cout<<Array1[Num_Elem]<<" ";       //tests that the array was filled corectly
    		Num_Elem--;
    	}
    
    	int compare (const void * a, const void * b)
    	{
    	 return ( *(int*)a - *(int*)b );                                
    	}
    
    	qsort (Array1, 24, sizeof(int), compare);
    
    }
    int main()
    {
    	ifstream Filein;
    	Filein.open("G:\\IntFile.dat");
    	int count = 0;
    	int Array1[100];
    
    	while(Filein)
    	{
    		Filein>>Array1[count];
    		count++;
    	}
    	Sort_Array(Array1, count);
    
    return 0;
    }
    I used a loop to make sure the array was filled correctly, and it was. I was trying to follow an example online, but i can't get it to work.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You must come from a different language. In C++ you can't define a function within a function. Move the compare definition above your function.

    BTW, that "typical" solution for your compare function is subject to undefined behavior.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    I'm not sure what you mean. How am i defining a function within a function? I was trying to follow this example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int values[] = { 40, 10, 100, 90, 20, 25 };
    
    int compare (const void * a, const void * b)
    {
      return ( *(int*)a - *(int*)b );
    }
    
    int main ()
    {
      int * pItem;
      int n;
      qsort (values, 6, sizeof(int), compare);
      for (n=0; n<6; n++)
         printf ("%d ",values[n]);
      return 0;
    }
    I tryed moving it around like this, but it still gives me errors.:
    Code:
    qsort (Array1, 24, sizeof(int), compare);
    
    int compare (const void * a, const void * b)
    	{
    	 return ( *(int*)a - *(int*)b );
    	}
    When i move :
    Code:
    qsort (Array1, 24, sizeof(int), compare);
    into main like the example, it says that compare is an undeclared identifier.
    The example dosn't use a template, so i'm guessing that's why following the example didn't work out. I'm still not sure how to fix the errors.

    **EDIT**
    I moved:
    Code:
    int compare (const void * a, const void * b);
    	int compare (const void * a, const void * b)
    	{
    	 return ( *(int*)a - *(int*)b );
    	}
    before the template, and it works. So i guess the question is how to implement it into the template.

    PS:
    Not from a different language, I'm just new to templates and sorting functions. I'm in two different programing classes, and i'm learining the sorting functions in one and templates in the other. So, i'm a little confused when trying to use both for the first time.
    Last edited by Cpro; 04-14-2007 at 09:03 PM.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This is defining a function within a function:
    Code:
    template<typename Type>
    void Sort_Array(Type *Array1, int Num_Elem)
    {
    	Num_Elem = Num_Elem - 2;
    	while(Num_Elem >= 0)
    	{
    		cout<<Array1[Num_Elem]<<" ";       //tests that the array was filled corectly
    		Num_Elem--;
    	}
    
    	int compare (const void * a, const void * b)
    	{
    	 return ( *(int*)a - *(int*)b );                                
    	}
    
    	qsort (Array1, 24, sizeof(int), compare);
    
    }
    This isn't:
    Code:
    int compare (const void * a, const void * b)
    {
       return( *(int*)a - *(int*)b );                                
    }
    
    template<typename Type>
    void Sort_Array(Type *Array1, int Num_Elem)
    {
       Num_Elem = Num_Elem - 2;
       while ( Num_Elem >= 0 )
       {
          cout<<Array1[Num_Elem]<<" ";       //tests that the array was filled corectly
          Num_Elem--;
       }
    
       qsort (Array1, 24, sizeof(int), compare);
    
    }
    And this
    Code:
    return( *(int*)a - *(int*)b );
    may or may not be correct.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Oh, i see what you mean now. I was thinking that the template function wasn't a function. Thank you for the explanation. I now have it working.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, there is a template function that does exactly what you need and unsurprisingly it is called sort(). So if qsort is OK and you can just write a wrapper around it, might just as well use std::sort.

    Your function does have lots of problems though. In addition to the ones mentioned above, what you are doing with the Num_Elem argument (-2 ?), then losing it and finally determining that the size of the array is 24 is a bit bizarre.

    Compare needs to be a template too, but unfortunately your idea won't work for strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Template function specialization
    By thomas.behr in forum C++ Programming
    Replies: 4
    Last Post: 06-03-2003, 11:50 AM