部分特化不特化任何模板参数

Dav*_*ica 1 c++ templates partial-specialization

我有以下代码,其中我试图制作模板化的安全数组迭代器。

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;
};
Run Code Online (Sandbox Code Playgroud)

但是,当我编译时,我得到 -

array.h:16: error: partial specialization ‘SArrayIterator<T>’ does not specialize any template arguments
Run Code Online (Sandbox Code Playgroud)

我不确定这意味着什么。我的猜测是它说我声明了一个 T 但我从不使用它,但这显然不是这种情况。

Pub*_*bby 5

这是正确的代码:

template <typename T>
class SArrayIterator {
Run Code Online (Sandbox Code Playgroud)

当您编写时class SArrayIterator<T>,编译器认为您将专门化模板,但您不是在这种情况下,因此您必须将其<T>排除在外。

您实际上也可以将<T>out 留在类主体中,例如:

SArrayIterator operator ++(){
Run Code Online (Sandbox Code Playgroud)