定义模板类,运算符和迭代器时遇到麻烦

时间:2013-04-13 21:51:53

标签: c++ templates iterator operators

所以我试图定义一个模板类“TwoWayVector”和“TwoWayVectorIterator”,但我遇到了很多问题。我想在TwoWayVector中定义==以返回引用并将const作为参数作为const TwoWayVector,这就是我如何定义其他运算符并且g ++没有抱怨,但由于某种原因,下面的代码会产生错误

TwoWayVector.cc: In member function ‘bool& TwoWayVector<T>::operator==(TwoWayVector<T>)         [with T = int]’:
Test.cc:10:   instantiated from here
TwoWayVector.cc:40: error: passing ‘const TwoWayVector<int>’ as ‘this’ argument of ‘T&       TwoWayVector<T>::operator[](int) [with T = int]’ discards qualifiers
Test.cc:10:   instantiated from here
TwoWayVector.cc:32: warning: reference to local variable ‘result’ returned

Test.cc

#include <iostream>
#include "TwoWayVector.cc"
int main(){
TwoWayVector<int> numbers;
TwoWayVector<int> numbers2;
numbers.push_back(3);
numbers.push_back(2);
numbers2.push_back(3);
numbers2.push_back(2);
cout << (numbers==numbers2);
cout << endl;
return 0;
}

TwoWayVector.cc

using namespace std;
#include "TwoWayVectorIterator.cc"
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];
}
bool& operator==(const TwoWayVector vector2){
    bool result = true;
    if(capacity != vector2.capacity){
        result = false;
    }
    if(nextFree != vector2.nextFree){
        result = false;
    }
    for(int i=0; i<nextFree ; i++){
        if(data[i] != vector2[i]){
            result = false;
        }
    }
    return result;
}
 //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<T>* begin(){
    TwoWayVectorIterator<T>* i = new TwoWayVectorIterator<T>(0,this);
    return (i);
}
TwoWayVectorIterator<T>* end(){
    TwoWayVectorIterator<T>* i = new TwoWayVectorIterator<T>(nextFree,this);
    return(i);
}

};

TwoWayVectorIterator.cc

#include <sstream>

using namespace std;

template<typename T> class TwoWayVector;

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 contents, position;
    contents = (vector == vector2) ? true : false;
    position =(currentPosition == vector2->currentPosition) ? true : false;
    return (contents && position);
}

bool& operator!=(const TwoWayVectorIterator vector2){
    bool contents, position;
    contents = (vector == vector2) ? false : true;
    position=(currentPosition == vector2->currentPosition) ? false : true;
    return (contents || position);
}

TwoWayVectorIterator& operator++(){
    return *this;
    currentPosition = (currentPosition+1);

}
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];
}
};

如果我将TwoWayVector.cc中的==运算符定义更改为

bool operator==(TwoWayVector vector2){
    bool result = true;
    if(capacity != vector2.capacity){
        result = false;
    }
    if(nextFree != vector2.nextFree){
        result = false;
    }
    for(int i=0; i<nextFree ; i++){
        if(data[i] != vector2[i]){
            result = false;
        }
    }
    return result;
}

然后一切都会编译,但是当我运行它时我得到了

1
a.out(40908) malloc: *** error for object 0x7fe4f2c03b40: pointer being freed was not   allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

任何想法?

2 个答案:

答案 0 :(得分:2)

operator==内,您在const变量operator[]上调用非{const vector2

您应该添加运算符的备用只读版本:

const T & operator[](const int index) const;

但是您应该使用对象参数的引用,如下所示:

bool operator==(const TwoWayVector &vector2) const;

否则,const关键字并没有真正起作用,因为它只表示您刚刚在函数堆栈上创建的对象副本无法修改,这并不重要。 (因此解决这个问题的最简单方法是从vector2中删除const关键字,但这不是很正确。)

当然,不要将bool值作为引用返回,因为它引用变量result,一旦离开函数就会不再存在。

答案 1 :(得分:1)

由于缺少复制构造函数,基本上会发生崩溃。见rule of three。什么时候

bool operator==(TwoWayVector vector2){

在main中使用,有创建的向量副本,从数字2保持指针,然后在退出表单操作符==,复制被删除,留下带有已释放内存指针的numbers2,它尝试在主要退出时删除(),这会导致错误。