2013-05-27 64 views
0

我想創建一個模板向量,但是當我返回迭代器時出現問題。 我一直在搜索所有的互聯網,並沒有找到任何解決方案。 下面的代碼:返回模板類C++的方法

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

我離開了一些方法實現保持代碼的短,我加了註釋,我得到的錯誤,我不知道爲什麼。如果沒有「迭代器」方法,代碼工作得很好。

+0

什麼錯誤?爲什麼所有這些代碼?告訴我們你的[testcase](http://sscce.org)。 **你可能需要'typename'。** –

+1

在class VectorDinamic {'?之前是否有一行'template '丟失了?否則,如何定義「元素」? – dyp

+0

對不起...等一下,我會編輯 –

回答

2

您錯過了vectorDinamicIterator的(向前)聲明。每個名稱在使用之前都必須聲明。

template < class Element > 
class vectorDinamicIterator; 

template <class Element> 
class VectorDinamic { 
public: 
    VectorDinamic(); 
    void add(Element el); 
    Element get(int poz); 
    ~VectorDinamic(); 
    void update(Element el,int poz); 
    int len(); 

    vectorDinamicIterator<Element>* iterator(); // <- name must be known 
    void del(Element el); 

private: 
    Element* elems; 
    int lg; 
    int capacitate; 
    void resize(); 
}; 

template <class Element> 
class vectorDinamicIterator{ 
public: 
    typedef VectorDynamic<Element> Vector; //<---- specify template argument! 

    vectorDinamicIterator(Vector* vect){ 
     this->curent=0; 
     this->vec = vect; 
    } 
    Element element() 
    { 
     return vec->get(this->curent); 
    } 
    void next(){ 
     curent++; 
    } 
private: 
    int curent; 
    Vector* vec; 
}; 

蔭Marcovic是正確的,你還需要在VectorDinamic<Element>::iterator()定義指定模板參數:

template <class Element> 
VectorDinamic<Element>::VectorDinamic() { 
    capacitate = INIT_CAPACITY; 
    elems = new Element[capacitate]; 
    lg = 0; 
} 

template <class Element> 
vectorDinamicIterator<Element>* VectorDinamic<Element>::iterator() 
{ 
    vectorDinamicIterator<Element>* iter = 
     new vectorDinamicIterator<Element>(this); 
    return iter; 
    // or simply 
    // return new vectorDinamicIterator<Element>(this); 
    // but I don't see why you return a pointer to a dynamically allocated 
    // iterator, instead of just an iterator 
} 
1

您沒有在變量聲明及其構造函數中指定模板。

要解決,只是return new vectorDinamicIterator<Element>(this);

編輯:噢,並作爲DYP注意到,你缺乏預先聲明。由於某些原因,我沒有把目光投向那部分。

+0

對不起...它不工作:(......感謝您的幫助 –

+0

你得到的錯誤是什麼?請在這裏粘貼。 –

+0

順便說一句,在你的代碼中,你沒有像我一樣現在你將指針指定給一個棧變量,如果你不想立即返回它,你需要將它聲明爲一個指針。 –