c++ - 部分特化不特化任何模板参数

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

但是当我编译时我得到 -

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

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

最佳答案

这是正确的代码:

template <typename T>
class SArrayIterator {

当你写 class SArrayIterator<T>编译器认为你要专门化模板,但在这种情况下你不是,所以你必须离开 <T>出。

您实际上可以离开 <T>也出现在类里面,例如:

SArrayIterator operator ++(){

关于c++ - 部分特化不特化任何模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13404091/

相关文章:

c++ - 使用带有类错误的 map ,编译错误

c++ - boost 二进制 static_visitor 和 apply_visitor

混合模板类和模板方法时出现 C++ 错误

c++ - 模板参数列表太少问题

c++ - 模板类构造函数的特化

c++ - 我需要对 Stroustrup 关于 ADL 的新书上的这个例子做一些澄清

c++ - 显示今天的日期

c++ - 从字符串变量执行 C++

c++ - 有没有办法从模板参数和字符串中生成模板类中的类型

c++ - 具有非类型参数的成员函数的部分特化