名前が示すように、TwoWayVector と TwoWayVectorIterator の 2 つのクラスがあります。独自のベクター クラスとその反復子を実装しようとしています。私はいくつかの可視性の問題を抱えているようで、メソッド TwoWayVector.begin(); から TwoWayVectorIterator を構築する方法もわかりません。
TwoWayVector.cc
#include <sstream>
using namespace std;
template <class T> class TwoWayVector{
public:
T* data;
int capacity;
int nextFree;
TwoWayVector(){
capacity = 10;
nextFree = 0;
data = new T[capacity];
}
~TwoWayVector(){
delete data;
}
T& operator[](const int index){
if( index >= capacity || capacity + index < 0){
string number = static_cast<ostringstream*>( &(ostringstream() << index) )->str();
string error = "index " + number + " is out of bounds";
throw error;
}
else if(index < 0){
return data[nextFree+index];
}
return data[index];
}
//memory leaks?
void push_back(T object){
if(capacity <= nextFree){
capacity = capacity*2;
T* tmp = new T[capacity];
for(int i=0; i<capacity; i++){
tmp[i] = data[i];
}
delete data;
data = tmp;
}
data[nextFree] = object;
nextFree++;
}
T pop_back(){
nextFree--;
T result = data[nextFree];
data[nextFree] = NULL;
return result;
}
int size(){
return nextFree;
}
TwoWayVectorIterator begin(){
TwoWayVectorIterator<T> iterator= new TwoWayVectorIterator<T>(0,this);
return (iterator);
}
};
TwoWayVectorIterator.cc
using namespace std;
template <class T> class TwoWayVectorIterator{
public:
TwoWayVector<T>* vector;
int currentPosition;
TwoWayVectorIterator(TwoWayVector<T>& vec){
currentPosition = 0;
vector = vec;
}
TwoWayVectorIterator( int pos , TwoWayVector<T>& vec){
currentPosition = pos;
vector = vec;
}
bool& operator==(const TwoWayVectorIterator* vector2){
bool address, position;
address = (&vector == &vector2) ? true : false;
position =(currentPosition == vector2->currentPosition) ? true : false;
return (address && position);
}
bool& operator!=(const TwoWayVectorIterator* vector2){
bool address, position;
address = (&vector == &vector2) ? true : false;
position=(currentPosition == vector2->currentPosition) ? true : false;
return (address && position);
}
TwoWayVectorIterator& operator++(){
currentPosition = (currentPosition+1);
return *this;
}
TwoWayVectorIterator& operator++(int){
currentPosition = (currentPosition+1);
return *this;
}
TwoWayVectorIterator& operator=(TwoWayVectorIterator* vector2){
&vector = vector2;
currentPosition = vector2->currentPosition;
return *this;
}
TwoWayVectorIterator& operator+(int n){
currentPosition = currentPosition+n;
return *this;
}
TwoWayVectorIterator& operator-(int n){
currentPosition = currentPosition-n;
return *this;
}
bool& operator<(TwoWayVectorIterator* vector2){
return (currentPosition<vector2->currentPosition);
}
T& operator*(){
return vector[currentPosition];
}
};
Test.cc から呼び出される
using namespace std;
#include <iostream>
#include "TwoWayVector.cc"
#include "TwoWayVectorIterator.cc"
int main(){
TwoWayVector<int> numbers;
numbers.push_back(3);
numbers.push_back(2);
numbers.size();
TwoWayVectorIterator current = numbers.begin();
return 0;
}
コンパイラ エラー:
In file included from Test.cc:3:
TwoWayVector.cc:59: error: ‘TwoWayVectorIterator’ does not name a type
Test.cc: In function ‘int main()’:
Test.cc:18: error: missing template arguments before ‘current’
Test.cc:18: error: expected `;' before ‘current’
いくつかの異なる方法、異なる包含スキームを宣言し、 TwoWayVectorIterator current = numbers.begin() を呼び出してみましたが、イテレータの型を指定する必要はありません。
ここで何か助けていただければ幸いです!!