ISO C ++禁止声明'TwoWayVectorIterator'没有类型

时间:2013-04-14 01:16:11

标签: c++ vector iterator declaration

我正在尝试实现一个向量,它可以在不使用库的情况下使用c ++中的正负索引(从后面计算,如python),并使用迭代器。我遇到了这个问题:当我g++ TwoWayVectorIterator.cc时,我会得到ISO C++ forbids declaration of ‘TwoWayVectorIterator’ with no typeerror: expected ‘;’ before ‘<’ token 。我在.h文件中尝试了很多东西作为分离模板但是没有用。 :(任何帮助都会非常感激!

在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

1 个答案:

答案 0 :(得分:0)

你有一种循环依赖。您可以通过在TwoWayVectorIterator顶部声明TwoWayVector.cc来解决此问题。然后不在TwoWayVectorIterator.cc

中加入TwoWayVector.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;
}