2012-05-12 12 views
2

C++関数を改訂する際に問題が発生し、どのデータ型でも機能するようになりました。すべての助けが大いに評価されるでしょう。以下は私のコードであり、私が実行し続けるエラーです。 quicksort()関数内pivotPoint = partition(set, start, end);を読み込みラインについてはC++:関数テンプレートの問題

// This program demonstrates the QuickSort Algorithm. 
#include <iostream> 
#include <algorithm> 
using namespace std; 


//************************************************ 
// quickSort uses the quicksort algorithm to  * 
// sort set, from set[start] through set[end]. * 
//************************************************ 

template <class T> 
void quickSort(T set[], int start, int end) 
{ 
    T pivotPoint; 

    if (start < end) 
    { 
     // Get the pivot point. 
     pivotPoint = partition(set, start, end); 
     // Sort the first sub list. 
     quickSort(set, start, pivotPoint - 1); 
     // Sort the second sub list. 
     quickSort(set, pivotPoint + 1, end); 
    } 
} 

//********************************************************** 
// partition selects the value in the middle of the  * 
// array set as the pivot. The list is rearranged so  * 
// all the values less than the pivot are on its left  * 
// and all the values greater than pivot are on its right. * 
//********************************************************** 

template <class T1> 
int partition(T1 set[], int start, int end) 
{ 
    int pivotValue, pivotIndex, mid; 

    mid = (start + end)/2; 
    swap(set[start], set[mid]); 
    pivotIndex = start; 
    pivotValue = set[start]; 
    for (int scan = start + 1; scan <= end; scan++) 
    { 
     if (set[scan] < pivotValue) 
     { 
     pivotIndex++; 
     swap(set[pivotIndex], set[scan]); 
     } 
    } 
    swap(set[start], set[pivotIndex]); 
    return pivotIndex; 
} 


//********************************************** 
// swap simply exchanges the contents of  * 
// value1 and value2.       * 
//********************************************** 

template <class T> 
void swap(T &value1, T &value2) 
{ 
    int temp = value1; 

    value1 = value2; 
    value2 = temp; 
} 

int main() 
{ 
    const int SIZE = 10; // Array size 
    int count;   // Loop counter 

    // need to override the [] function? 
    int array[SIZE] = {7, 3, 9, 2, 0, 1, 8, 4, 6, 5}; 

    // Display the array contents. 
    for (count = 0; count < SIZE; count++) 
     cout << array[count] << " "; 
    cout << endl; 

    // Sort the array. 
    quickSort(array, 0, SIZE - 1); 

    // Display the array contents. 
    for (count = 0; count < SIZE; count++) 
     cout << array[count] << " "; 
    cout << endl; 
    return 0; 
} 

、私はこのエラーが表示されます。

main.cpp:24: error: no matching function for call to 'partition(int*&, int&, int&)' 

誰かが私にはそれが何を意味するのか知っている可能性があり、それに合わせてどのようにした場合、私は非常になります感謝する

+8

アルゴリズム[ 'のstd :: partition'](http://en.cppreference.com/wが既にあります/ cpp /アルゴリズム/パーティション)。 「乱用する名前空間std;」を止め、あなた自身でそれを書き出してください。 –

+0

クイック返信ありがとうございます@KerrekSB。私はあなたが言っていることを完全に理解しているかどうかはわかりません。 –

+0

関数の呼び出し順序を考える必要があります。 'quickSort'は定義される前に' partition'関数を呼び出し、 'partition'はそれが定義される前に' swap'関数を呼び出します。また、 'pivotPoint'型は' T'ではなく、あなたの場合は 'int'でなければなりません。これが宿題でない場合は、 'std :: swap'、' std :: partition'(さらに 'std :: sort')を使って見てください。 – Chad

答えて

4

partition関数は、使用する前に宣言する必要があります。

quicksort定義の前に partition定義を移動するか、その場所に宣言を追加

template <class T1> 
int partition(T1 set[], int start, int end); 
+0

ありがとうございました! –

関連する問題