2

I have the following code in which Im trying to make a templated safe array iterator.

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;
};

However when I compile I get -

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

and Im not sure what that means. My guess was that its says that I declare a T but I never use it, but this obviously isnt the case.

4

2 に答える 2

6

これは正しいコードです:

template <typename T>
class SArrayIterator {

あなたが書くときclass SArrayIterator<T>、コンパイラはあなたがテンプレートを特殊化しようとしていると考えますが、この場合はそうではないので、除外する必要があります<T>

実際に<T>は、クラス本体でも out を省略できます。たとえば、次のようになります。

SArrayIterator operator ++(){
于 2012-11-15T18:48:25.520 に答える
1

部分的な特殊化の構文でベース テンプレートを記述します。基本テンプレートの正しい宣言は次のとおりです。

template <typename T>
class SArrayIterator {

特殊な宣言は次のようになります

template <>
class SArrayIterator<double> {
于 2012-11-15T18:50:02.167 に答える