模板类中的可见性

时间:2013-04-06 15:18:40

标签: c++ templates iterator

我有两个类,TwoWayVector和TwoWayVectorIterator,顾名思义,我正在尝试为它实现自己的vector类和迭代器。我似乎有一些可见性问题,而且我也不确定如何从方法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(),但我不想指定迭代器的类型。

非常感谢任何帮助!!

1 个答案:

答案 0 :(得分:4)

第一个问题:

您似乎没有#include包含文件TwoWayVectorIteratorTwoWayVector.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(至少判断你是如何使用它的):< / p>

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)。选择myVectorinnerVector之类的内容,或您认为最适合作为描述性名称的内容。

此外,您应遵循命名约定并分别提供头文件和实现文件扩展名,例如.h(或.hpp)和.cpp