ライブラリとイテレータを使用せずに、C ++で正と負のインデックス(Pythonのように後ろからカウント)を取ることができるベクトルを実装しようとしています。私はこの問題に出くわしg++ TwoWayVectorIterator.cc
ましISO C++ forbids declaration of ‘TwoWayVectorIterator’ with no type
たerror: 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