0

我有两个类,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() ,但我不想指定迭代器的类型。

非常感谢这里的任何帮助!

4

1 回答 1

4

第一个问题:

似乎您还没有#included 包含TwoWayVectorIteratorfrom file 定义的文件TwoWayVector.cc,该文件在 function 中使用其定义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当函数返回时,该引用最终将被销毁,给您留下一个悬空引用,并在您尝试取消引用它时立即导致未定义的行为。

您应该只返回 a 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.

于 2013-04-06T15:32:19.410 回答