2013-04-14 55 views
0

我想實現一個向量,可以在C++中使用正向和負向索引(從後面開始計數),而不使用庫和迭代器。我碰到這個問題:當我g++ TwoWayVectorIterator.cc,我得到這個ISO C++ forbids declaration of ‘TwoWayVectorIterator’ with no typeerror: expected ‘;’ before ‘<’ token 。我在.h文件中嘗試了很多分離模板的方法,但沒有奏效。 :(任何幫助,將這麼多的讚賞!ISO C++禁止聲明沒有類型的'TwoWayVectorIterator'

在TwoWayVector.cc

#ifndef TWOWAYVECTOR_CC 
#define TWOWAYVECTOR_CC 

#include"TwoWayVectorIterator.cc" 
#include <iostream> 
#include <cmath> 
using namespace std; 

template <typename T> class TwoWayVector 
{ 
private: 
    T* data; 
    int capacity; 
    int nextFree; 
public: 
    TwoWayVector(); 
    ~TwoWayVector(); 
    int size(); 
    void push_back(T); 
    T pop_back(); 
    void checkIndex(int index); 
    T& operator[] (int index); 
    TwoWayVectorIterator<T> begin(); 
    TwoWayVectorIterator<T> end(); 
    //TwoWayVectorConstIterator<T> const_begin(); 
    //TwoWayVectorConstIterator<T> const_end(); 

}; 

template<typename T> TwoWayVector<T>::TwoWayVector(){ //construct function 
    data = new T[10] ; // initial memory storage 
    capacity=10; 
    nextFree=0; 
} 

template<typename T> 
TwoWayVector<T> ::~TwoWayVector(){ 
    delete []data; 
    //delete capacity; 
    //delete nextFree; 
} 

template<typename T> 
int TwoWayVector<T> ::size(){ 
    return nextFree; 
} 

template<typename T> void TwoWayVector<T> ::push_back(T element){ 
    if(this->size() == this->capacity){ 
     T* data2 = new T[2 * this->capacity]; 
     int capacity2 = 2*this->capacity; 
     int nextFree2 =0; 
     int i; 
     for (i=0; i<this->capacity; i++) { 
      data2[i] = this->data[i]; 
     } 
     data2[this->nextFree] = element; 
     nextFree2 =this->nextFree+1; 
     T * temp = this->data; 
     delete []temp; // release original memory 
     this->data = data2; 
     this->capacity = capacity2; 
     this->nextFree = nextFree2; 

    }else{ 
     this->data[this->nextFree] = element; 
     nextFree++; 
    } 
} 

template<typename T> T TwoWayVector<T> ::pop_back(){ 
    if(this->size()>0){ 
     T popped = this->data[(this->nextFree)-1]; 
     this->data[(this->nextFree)-1] = 0; 
     this->nextFree --; 
     return popped; 
    }else{ 
     return 0; 
    } 
} 

template<typename T> void TwoWayVector<T> ::checkIndex(int index) { 

    if((index>0 && (index+1)>this->size())||(index<0 && abs(index)> this->size())) 
    { 
     throw index; 
    } 
} 

template<typename T> T& TwoWayVector<T> :: operator[] (int index){ 
    try{ 
    checkIndex(index); 
    } 
    catch (int) 
    { 
     cerr <<"error: the index " <<index<<" is not in the valid range." << endl; 
     exit(1); 
    } 
    if(index>=0) 
     return data[index]; 
    else 
     return data[nextFree + index]; 
} 

template<typename T> TwoWayVectorIterator<T> TwoWayVector<T> ::begin(){ 
    TwoWayVectorIterator *a = new TwoWayVectorIterator<T>(&this, 0); 
    return *a; 
} 

template<typename T> TwoWayVectorIterator<T> TwoWayVector<T> ::end(){ 
    TwoWayVectorIterator *a = new TwoWayVectorIterator<T>(&this, this.size()); 
    return *a; 
} 

#endif 

然後在TwowayVectorIterator.cc

/* 
* TwoWayVectorIterator.cc 
* assig4 
* 
* Created by yunjing tian on 13-04-09. 
* 
*/ 
#ifndef TWOWAYVECTORITERATOR_CC 
#define TWOWAYVECTORITERATOR_CC 

#include"TwoWayVector.cc" 
using namespace std; 

template <typename T> class TwoWayVectorIterator 
{ 
private: 
    TwoWayVector<T> * vector; 
    int currentPosition; 
public: 

    TwoWayVectorIterator(TwoWayVector<T> &a, int initialPosition); 
    bool operator== (TwoWayVectorIterator b); 
    bool operator!= (TwoWayVectorIterator b); 
    TwoWayVectorIterator<T> & operator++(); 
    TwoWayVectorIterator<T> & operator++(TwoWayVectorIterator a); 
    TwoWayVectorIterator<T> & operator=(TwoWayVectorIterator a); 
    TwoWayVectorIterator<T> & operator+(int i); 
    TwoWayVectorIterator<T> & operator-(int i); 
    bool operator<(TwoWayVectorIterator b); 
    TwoWayVector<T>& operator*(); 
}; 

template<typename T> TwoWayVectorIterator<T>::TwoWayVectorIterator(TwoWayVector<T> &a, int initialPosition){ //construct function 
    vector = a ; 
    currentPosition = initialPosition; 
} 

template<typename T> bool TwoWayVectorIterator<T> :: operator== (TwoWayVectorIterator b){ 
    if (this.vector == b.vector && this.currentPosition == b.currentPosition) { 
     return true; 
    } 
    else { 
     return false; 
    } 

} 

template<typename T> bool TwoWayVectorIterator<T> :: operator!= (TwoWayVectorIterator b){ 
    if (this.vector != b.vector || this.currentPosition != b.currentPosition) { 
     return true; 
    } 
    else { 
     return false; 
    } 

} 


template<typename T> TwoWayVectorIterator<T>& TwoWayVectorIterator<T> ::operator++(){ 

    currentPosition++; 
    return *this; 
} 

template<typename T> TwoWayVectorIterator<T>& TwoWayVectorIterator<T> ::operator++(TwoWayVectorIterator a){ 

    ++a.currentPosition; 
    return *a; 
} 

template<typename T> TwoWayVectorIterator<T>& TwoWayVectorIterator<T> ::operator=(TwoWayVectorIterator other){ 
    vector = other.vector; 
    currentPosition = other.currentPosition; 
    return *this; 
} 

template<typename T> TwoWayVectorIterator<T>& TwoWayVectorIterator<T> ::operator+(int i){ 
    currentPosition= currentPosition+i; 
    return *this; 

} 

template<typename T> TwoWayVectorIterator<T>& TwoWayVectorIterator<T> ::operator-(int i){ 
    currentPosition= currentPosition-i; 
    return *this; 

} 

template<typename T> bool TwoWayVectorIterator<T> :: operator<(TwoWayVectorIterator b){ 
    if(this.currentPosition < b.currentPosition) return true; 
    else return false; 

} 

template<typename T> TwoWayVector<T>& TwoWayVectorIterator<T> ::operator*(){ 
    return (*vector)[ currentPosition ]; 
} 


/*int main(){ 
    TwoWayVector<int> numbers; 
    numbers.push_back(3); 
    numbers.push_back(2); 
    for (TwoWayVectorIterator current = numbers.begin(); 
     current != numbers.end(); 
     current++) 
    { 
     cout << *numbers; 
    } 
}*/ 

#endif 
+0

模板類應該聲明和定義(實現)的頭文件。閱讀[這裏](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file)。 –

+0

不要包含cc/cpp文件。不要在頭文件中使用名稱空間標準。不要讓文件循環依賴於自己。確保你瞭解「翻譯單位」和「單一定義規則」的概念。確保你明白#include是一個簡單的文本替換。你也可以谷歌「獨立編譯」。或者只是一本體面的C++書籍來解釋所有這些。 – sellibitze

回答

0

你有一種循環dependencey的。你可以解決它向前宣佈TwoWayVectorIteratorTwoWayVector.cc之上,然後不包括TwoWayVectorIterator.ccTwoWayVector.cc

#ifndef TWOWAYVECTOR_CC 
#define TWOWAYVECTOR_CC 

// #include"TwoWayVectorIterator.cc" 
^^^^^^^^^^^^^^^^^^^^^^ 

#include <iostream> 
#include <cmath> 
using namespace std; 

template <typename T> 
class TwoWayVectorIterator; 
^^^^^^^^^^^^^^^^^^^^^^ 

template <typename T> class TwoWayVector 

而你在這裏失去了模板參數

template<typename T> TwoWayVectorIterator<T> TwoWayVector<T> ::begin(){ 
    TwoWayVectorIterator<T> *a = new TwoWayVectorIterator<T>(&this, 0); 
         ^^^ 
    return *a; 
} 

template<typename T> TwoWayVectorIterator<T> TwoWayVector<T> ::end(){ 
    TwoWayVectorIterator<T> *a = new TwoWayVectorIterator<T>(&this, this.size()); 
         ^^^ 
    return *a; 
} 
+0

非常感謝你!有效!我還添加了'template class TwoWayVector;'在'TwoWayVectorIterator.cc'中。 :D:D – user2041496

+0

@ user2041496很高興幫助:)。 – 2013-04-14 01:32:28

+0

@ user2041496順便說一句,看看編輯。我糾正了一些東西。 – 2013-04-14 01:33:07

相關問題