我有以下代码,我在其中尝试制作模板化安全数组迭代器。
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/