2013-04-11 100 views
2

如果這是一個非常基本的問題,我很抱歉,我對C++很陌生。 我試圖爲它定義我自己的矢量類和迭代器。但是,只要我重載一個操作符,返回的值總是一個地址。重載運算符的返回值

例如,下面的代碼打印0x7fb6dbc000e0 0x7fb6dbc000e0時,我想它打印1 0

因爲我已經和語法一會兒插科打諢,部分經營者看起來有些不同,這僅僅是如此你可以看到我嘗試過的一些東西。

test.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; 
} 

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){ 
    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]; 
} 
}; 
+0

那些''cout''是什麼意思? – gongzhitaao 2013-04-11 15:21:23

+0

http://en.cppreference.com/w/cpp/language/operator_precedence – BoBTFish 2013-04-11 15:22:08

+0

如果您希望使用爲您的迭代器類定義的'operator ==',則必須取消引用實例指針:'(* beginning ==開始)'。 – didierc 2013-04-11 15:38:14

回答

5

< <運算符的優先級高於==和!=運算符的precedence。所以

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

的真正含義

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

嘗試 COUT < <(開始== beginning2); cout < <(beginning)!= beginning2);