2014-02-16 107 views
0

我已經爲類創建了實例化文件,但我很好奇,我如何創建一個只是一個函數。這是我製作的一個程序的一個例子。模板函數實例化文件?

template <typename T> 
void deSelSort(T arr[], int n) 
{ 
int j = n - 1; 

for (int i = 0; i < n; i++) 
    { 
     cout << arr[i] << " "; 
    } 
    cout << endl; 

for(int i = 0; i < j; i++, j--) { 

    int min=i; 
    int max=i; 
    int temp; 

    for (int temp2 = i + 1; temp2 <= j; temp2++) 
    { 
     if (arr[temp2] < arr[min]) 
      { 
       min = temp2; 
      } 
     if (arr[temp2] > arr[max]) 
      { 
       max = temp2; 
      } 
    } 

    if (min > max) { 
     temp=min; 
     min=max; 
     max=temp; 
     arr[min] = max; 
     arr[max] = min; 
    } 
    if (min > i) 
    { 
     temp = arr[i]; 
     arr[i] = arr[min]; 
     arr[min] = temp; 
    } 
    if (max < j) 
    { 
     temp = arr[j]; 
     arr[j] = arr[max]; 
     arr[max] = temp; 
    } 

    for (int i = 0; i < n; i++) 
    { 
     cout << arr[i] << " "; 
    } 
    cout << endl; 


}; 
} 

如果我想用例如int和double來創建一個數組,實例化文件會是什麼樣子?現在我的文件看起來像這樣

#include "ex3_16.cpp" 

template class deSelSort<int>; 
template class deSelSort<double>; 
+0

'模板空隙deSelSort (INT [],int);在'和'模板空隙deSelSort (雙[],int);在' – 0x499602D2

回答

0

將實例化文件的樣子,如果我想創建intsdoubles例如數組是什麼?

template void deSelSort<int>(int[], int); 
template void deSelSort<double>(double[], int);