我有两个类,TwoWayVector 和 TwoWayVectorIterator,顾名思义,我正在尝试实现我自己的向量类和一个迭代器。我似乎遇到了一些可见性问题,而且我也不确定如何从方法 TwoWayVector.begin(); 构造一个 TwoWayVectorIterator;
双向矢量.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() ,但我不想指定迭代器的类型。
非常感谢这里的任何帮助!