2009-11-25 63 views
0

錯誤C2065:「溫度」:未聲明的標識符幫助的模板歸併功能

我知道,「TEMP」我需要聲明的是,數組是如int溫度[]但是如果類型我不知道它是什麼..它可能是intstringdouble ..我怎樣才能創建一個臨時數組不指定它應該是什麼

這裏是我的代碼:

template<class T> 
void Mergesort(T& a, int first, int last); 
template<class T> 
void Merge(T& a, int first, int last); 


int main() 
{ 
    int num; 

    // Read in an array of strings 
    cout << "How many words? "; 
    cin >> num; 
    Array<string> b(num); 
    cout << "Enter the " << num << " words below:\n"; 
    for (int i=0; i<num ; i++) 
     cin >> b[i]; 
    cout << "\nThank you!!\n"; 

    // Copy the original array and sort it using Quicksort 
    Array<string> bq(b); 
    Quicksort(bq, 0, num-1); 
    cout << "\nElements sorted using quicksort:\n"; 
    for (int i=0; i<num ; i++) 
     cout << bq[i]<< " "; 
    cout << "\n"; 

    // Print original list of elements. 
    cout << "\nOriginal elements:\n"; 
    for (int i=0; i<num ; i++) 
     cout << b[i]<< " "; 
    cout << "\n"; 

    // Copy original array and sort it using MergeSort 
    Array<string> bm(b); 
    Mergesort(bm, 0, num-1); 
    cout << "\nElements sorted using mergesort:\n"; 
    for (int i=0; i<num ; i++) 
     cout << bm[i]<< " "; 
    cout << "\n"; 


template<class T> 
void Mergesort(T& a, int first, int last) 
{ 
    if (first < last) 
    { 
     int mid = (first + last)/2; 
     Mergesort(a, first, mid); 
     Mergesort(a, mid+1, last); 
     Merge(a, first, last); 
    } 
} 

template<class T> 
void Merge(T& a, int first, int last) 
{ 
    int mid = (first + last)/2; 
    int one = 0, two = first, three = mid + 1; 
    T *temp = new T[last]; 

    while (two <= mid && three <= last) // Neither sublist is done 
     if (a[two] < a[three])   // Value in first half is smaller 
      temp[one++] = a[two++]; 
     else       // Value in second half is smaller 
      temp[one++] = a[three++]; 
    while (two <= mid)     // Finish copying first half 
     temp[one++] = a[two++]; 
    while (three <= last)    // Finish copying second half 
     temp[one++] = a[three++]; 
    for (one = 0, two = first; two <= last; a[two++] = temp[one++]); 
} 

//ARRAY.h 

using namespace std; 

template<class T> class Array 
{ 
public: 
    typedef T value_type; 
    Array(int s); 
    Array(int l, int h); 

    Array(const Array& other); 
    ~Array(); 

    T& operator[](int index); 
    const T& operator[](int index) const; 

    int get_size() const {return arraySize;} 

private: 
    int low; 
    int high; 
    int arraySize; //size of array 
    int offset; //to adjust back to an index of zero 
    T *array_; 

    void Copy(const Array&); 
}; 
+0

ok我試過,但現在我得到錯誤錯誤1錯誤C2512:'數組':沒有適當的默認構造函數可用和錯誤2錯誤C2679:二進制'=':找不到操作符,它需要一個類型的右側操作數'std :: string'(或者沒有可接受的轉換) – ritual 2009-11-25 22:24:38

+1

@ritual啊,我沒有仔細閱讀你的代碼。在Merge和Mergesort函數中,T實際上是數組類型,而不是元素類型,所以我認爲temp變量需要是'T :: value_type *'。對於那個很抱歉。 – 2009-11-25 22:58:07

+1

順便說一句,你正在泄漏代碼現在的'temp'分配。使用'Array '來保存臨時值而不是原始數組可能更有意義。 – 2009-11-25 23:00:10

回答

2

臨時應該是一個臨時項目的數組吧?嗯,這聽起來像你需要做的是這樣的:

typedef typename T::value_type U; 
U *temp = new U[count]; 

完成後不要忘記delete[]吧!另外,作爲一個附註,更多的C++ - ish要做的事情就是使用指針/迭代器。這樣,你的算法可以處理更多類型的容器,而不僅僅是模擬數組的東西。

此外,如Tim Sylvester建議,您可以使用std::vector<U> temp(count)這將爲您管理內存。

+2

考慮使用'std :: vector temp(count)'來代替,那麼即使在出現異常的情況下,您也不需要自行清理。 – 2009-11-25 21:30:37

3

在模板函數或類的上下文中,模板參數是「真實」類型。 temp所需的類型僅爲T*

更新:

由於T是數組類沒有元素的類型,你真正想要T::value_type*臨時值。

+0

是的,只需要像使用其他任何類型一樣使用T即可 - 編譯器會針對每種用法針對您的具體情況。 – 2009-11-25 21:29:14

+0

確定我試過了,但現在我得到錯誤 錯誤錯誤C2512:「陣」:沒有可用的適當的默認構造函數 和 錯誤錯誤C2679:二進制「=」:沒有操作員發現這需要一個正確的類型'std :: string'的手操作數(或者沒有可接受的轉換) – ritual 2009-11-25 21:48:38