2

如果这是一个非常基本的问题,我很抱歉,我对 C++ 很陌生。我正在尝试为它定义自己的向量类和迭代器。但是,每当我重载运算符时,返回的值始终是地址。

例如,以下代码0x7fb6dbc000e0 0x7fb6dbc000e0在我想要打印时打印1 0

由于我一直在搞乱语法,一些运算符看起来有点不同,这只是为了让您可以看到我尝试过的一些东西。

测试.cc

#include <iostream>
#include "TwoWayVector.cc"
int main(){
    TwoWayVector<int> numbers;
    numbers.push_back(3);
    numbers.push_back(2);
        TwoWayVectorIterator<int>* beginning = numbers.begin();
    TwoWayVectorIterator<int>* beginning2 = numbers.begin();
    cout << beginning==beginning2;
    cout << beginning != beginning2;
    cout << endl;
return 0;
}

双向矢量.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){
    if(capacity != vector2->capacity){
        return false;
    }
    if(nextFree != vector2->nextFree){
        return false;
    }
    for(int i=0; i<nextFree ; i++){
        if(data[i] != vector2[i]){
            return false;
        }
    }
    return true;
}
 //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

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;
    cout << contents << endl;
    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];
}
};
4

2 回答 2

9
cout << beginning==beginning2;

并不意味着_

cout << (beginning==beginning2);

确实意味着

(cout << beginning) == beginning2;

http://en.cppreference.com/w/cpp/language/operator_precedence

因此,您正在打印 a TwoWayVectorIterator<int>*,而不是bool.

于 2013-04-11T15:23:14.753 回答
5

<< 运算符的优先级高于== 和 != 运算符的优先级。所以

cout << beginning==beginning2;
cout << beginning != beginning2;

真正意思

(cout << beginning)==beginning2;
(cout << beginning) != beginning2;

试试 cout << (beginning==beginning2); cout << (开始) != 开始2);

于 2013-04-11T15:23:53.233 回答