顾名思义,我有两个类TwoWayVector和TwoWayVectorIterator,我正在尝试实现自己的向量类和一个迭代器。我似乎遇到了一些可见性问题,并且我也不确定如何从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(),但是我不想指定迭代器的类型。
非常感谢您的任何帮助!!
最佳答案
第一个问题:
似乎您还没有从文件#include
中TwoWayVectorIterator
d包含TwoWayVector.cc
定义的文件,该文件在函数begin()
中使用其定义。
尝试添加以下指令:
#include <sstream>
#include "TwoWayVectorIterator.cc"
^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
第二个问题:
在
TwoWayVectorIterator.cc
内,您将必须提交前向声明以使其意识到TwoWayVector
的存在:template<typename T> class TwoWayVector;
第三个问题:
同样,在函数
begin()
中,您将TwoWayVectorIterator
用作返回类型,而没有指定必要的模板参数:TwoWayVectorIterator<T> begin(){
// ^^^
TwoWayVectorIterator<T> iterator= new TwoWayVectorIterator<T>(0,this);
return (iterator);
}
第四个问题:
您的
main()
函数遇到类似的问题:int main()
{
TwoWayVector<int> numbers;
// ...
TwoWayVectorIterator<int> current = numbers.begin();
// ^^^^^
// ...
}
第五题:
另一个问题是
TwoWayVectorIterator
的构造函数应接受指向TwoWayVector
的指针,而不是对其的引用(至少从使用方式上判断):TwoWayVectorIterator( int pos , TwoWayVector<T>* vec){
// ^^^^^^^^^^^^^^^^
currentPosition = pos;
vector = vec;
}
第六题:
operator ==
和operator !=
的重载将返回对临时bool
对象的引用,该对象最终将在函数返回时被销毁,留下悬挂的引用,并在尝试取消引用时导致未定义行为它。您应该只返回
bool
: bool operator==(const TwoWayVectorIterator* vector2){
// ^^^^
// Return by value here!
// ...
return (address && position);
}
bool operator!=(const TwoWayVectorIterator* vector2){
// ^^^^
// Return by value here!
// ...
return (address && position);
}
相关建议:
此外,您应该避免在全局名称空间范围使用这样的
using
指令:using namespace std;
这会将所有名称从
std
命名空间导入到全局命名空间中,从而可能导致不希望的名称冲突。作为进一步的一般建议,请避免为变量提供标准容器类的名称(例如
vector
)。选择诸如myVector
或innerVector
之类的名称,或者您认为最适合作为描述性名称的名称。此外,您应该遵循命名约定,并分别给您的头文件和实现文件扩展名,例如
.h
(或.hpp
)和.cpp
。