Search code examples
c++templatespartial-specialization

partial specialization does not specialize any template arguments


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.


Solution

  • This is the correct code:

    template <typename T>
    class SArrayIterator {
    

    When you write class SArrayIterator<T> the compiler thinks you're going to specialize the template, but you're not in this case and so you have to leave the <T> out.

    You can actually leave the <T> out in the class body too, e.g.:

    SArrayIterator operator ++(){