0

我正在尝试在不使用库和迭代器的情况下在 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
4

1 回答 1

0

你有一种循环依赖。TwoWayVectorIterator您可以通过在TwoWayVector.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;
}
于 2013-04-14T01:20:33.803 回答