2012-11-15 75 views
1

我有下面的代碼,其中我試圖做一個模板化的安全數組迭代器。部分專精不專門化任何模板參數

template <typename T> 
class SArrayIterator; 

template <typename E> 
class SArray; 

class SArrayIteratorException; 

template <typename T> 
class SArrayIterator<T> {//<--line 16 
     friend std::ostream &operator <<(std::ostream &os, const SArrayIterator<T> &iter); 
public: 
     SArrayIterator<T>(SArray<T> &sArr) : beyondLast(sArr.length()+1), current(0), sArr(sArr){} 

     T &operator *(){ 
       if (current == beyondLast) throw SArrayIteratorException("Attempt to dereference 'beyondLast' iterator"); 
       return sArr[current]; 
     } 

     SArrayIterator<T> operator ++(){ 
       if (current == beyondLast) throw SArrayIteratorException("Attempt to increment 'beyondLast' iterator"); 
       current++; 
       return *this; 
     } 

     bool operator ==(const SArrayIterator<T> &other) {return sArr[current] == other.sArr[current];} 
     bool operator !=(const SArrayIterator<T> &other) {return !(*this == other);} 
private: 
     int first, beyondLast, current; 
     SArray<T> sArr; 
}; 

然而,當我編譯 -

array.h:16: error: partial specialization ‘SArrayIterator<T>’ does not specialize any template arguments 

和林不知道這意味着什麼。我的猜測是它說我聲明瞭一個T,但我從來沒有使用它,但顯然並非如此。

回答

3

這是正確的代碼:

template <typename T> 
class SArrayIterator { 

當你寫class SArrayIterator<T>編譯器認爲你要專門的模板,但你不是在這種情況下,所以你必須離開<T>出。

實際上,你可以離開<T>出在類體內過多,例如:

SArrayIterator operator ++(){ 
+0

唉唉!對。這是有道理的,它的工作原理。謝謝。 –

1

您編寫帶有部分專業化的語法基礎模板;對基本模板正確的聲明是:

template <typename T> 
class SArrayIterator { 

專門聲明如下

template <> 
class SArrayIterator<double> {