我想做一个模板向量,但是当我返回迭代器时出现问题。我一直在搜索所有互联网,但没有找到任何解决方案。这是代码:
template <class Element>
class VectorDinamic {
public:
VectorDinamic();
void add(Element el);
Element get(int poz);
~VectorDinamic();
void update(Element el,int poz);
int len();
//template <class Element>
//I get errors here: missing type specifier, unexpected token precedin ";" , missing ";" before "<"
vectorDinamicIterator<Element>* iterator();
void del(Element el);
private:
Element* elems;
int lg;
int capacitate;
void resize();
};
template <class Element>
class vectorDinamicIterator{
public:
vectorDinamicIterator(VectorDinamic* vect){
this->curent=0;
this->vec = vect;
}
Element element()
{
return vec->get(this->curent);
}
void next(){
curent++;
}
private:
int curent;
VectorDinamic* vec;
};
template <class Element>
VectorDinamic<Element>::VectorDinamic() {
capacitate = INIT_CAPACITY;
elems = new Element[capacitate];
lg = 0;
}
//I get errors down here: iterator is not a member of VectorDinamic<Element>,
template <class Element>
vectorDinamicIterator<Element>* VectorDinamic<Element>::iterator()
{
vectorDinamicIterator iter = new vectorDinamicIterator(this);
return iter;
}
我省略了一些方法实现以保持代码简短,我在出现错误的地方添加了注释,但我不知道为什么。没有“迭代器”方法,代码工作得很好。