2015-07-12 49 views
1

注意這是作業,但我們被允許並鼓勵尋求幫助,因爲我們的單一教授沒有時間迴應所有學生。如果因爲這個問題的功課性質而不願意幫忙,請不要回復,而應該尋求幫助。我不希望我的作業爲我完成。我只是想幫助理解我的錯誤所在。所有幫助表示讚賞!C++ 11基於陣列的散列:自動不循環

我正在處理基於數組的哈希表。我在散列中有一些值,我想檢查散列中所有元素長度的總和。散列中的元素是字符串。

我使用的後續代碼通過在散列值的每個成員進行迭代...

for (auto iter : myHash) { 
     //count up the length of all the strings 
     countOfItems += iter.length(); 
     cout << iter << " "; 
    } 

的問題是,在代碼永遠不會循環,一次也沒有。它從來沒有擊中countOfItems + = iter.length();

我調試了這個問題,並盡我所能,但仍然失去了我的迭代器。我將在這裏這些張貼在這裏...迭代器

template <typename KEY, typename VALUE> 
arrayHashIterator<KEY, VALUE> arrayHashTable<KEY, VALUE>::begin() const { 

    arrayHashIterator<KEY, VALUE> temp; 
    temp.keyArray = this->keyArray; 
    temp.valueArray = this->valueArray; 
    temp.statusArray = this->statusArray; 
    temp.index = 0; 
    temp.arraySize = this->arraySize; 
    temp.offTheRightEdge = false; 

    if (temp.statusArray[0] != 1) { 
     //Go search for the first index that contains useful data 
     ++temp; 
    } 
    return temp; 
} 

當代碼到達重載++運算符它進入這個其他類...

template <typename KEY, typename VALUE> 
arrayHashIterator<KEY, VALUE> arrayHashIterator<KEY, VALUE>::operator++() { 

    for(index; index < arraySize; index++){ 
     if(statusArray[index] == 1) 
     { 
      offTheRightEdge = false; 
      return *this; 
     } 
    } 
    offTheRightEdge = true; 
    return *this; 
} 

現在我調試和步該代碼正確地獲取重載的++運算符,然後找到存儲值的第一個索引,然後將該arrayHashIterator對象返回到begin(),然後將其返回。我希望它會有東西進入(Auto iter:Hash)循環,但它沒有。

我要針對arrayHashIterator類重載運算符*如下所述...

template <typename KEY, typename VALUE> 
VALUE& arrayHashIterator<KEY, VALUE>::operator*() const{ 

    if(offTheRightEdge == true){ 
     throw Error(); 
    } 
    return valueArray[index]; 
} 

我幾乎可以肯定我已經進入元素融入到我的散列正確的,因爲如果我打開我的數組值,以及作爲調試器中的鍵和狀態,我發現所有信息都以正確的形式出現在正確的位置。

我只是爲了(auto iter:hash)無法循環而感到茫然。我確實相信這個問題出現在我的重載++或重載操作符中,但我不能確定。

關於這個問題的第二雙眼睛將不勝感激。我不想要一些即時回答的代碼片段,我只是感謝一些幫助找到錯誤,以及我可以如何解決它!

編輯:有很多更多的代碼散列表和每個用例的檢查,但我想發佈特定部分到我的問題是。我可以根據要求提供代碼。

編輯:這是我爲我的end()方法以及我重載=操作...

更新:!重載!=

template <typename KEY, typename VALUE> 
bool arrayHashIterator<KEY, VALUE>::operator!=(const arrayHashIterator<KEY, VALUE>& right) const { 
    //TODO: see if the "this" iterator and the right iterator are not equal. 
    //To do this, check both iterators' index values and offTheRightEdge values 
    if(this->offTheRightEdge != right.offTheRightEdge || this->index != right.index) { 
     return true; 
    } else { 
     return false; 
    } 
} 

末()

template <typename KEY, typename VALUE> 
arrayHashIterator<KEY, VALUE> arrayHashTable<KEY, VALUE>::end() const { 

    arrayHashIterator<KEY, VALUE> temp; 
    temp.keyArray = this->keyArray; 
    temp.valueArray = this->valueArray; 
    temp.statusArray = this->statusArray; 
    temp.index = this->arraySize; 
    temp.arraySize = this->arraySize; 
    temp.offTheRightEdge = true; 

    return temp; 
} 
+0

'end()'怎麼樣? 'arrayHashIterator '的比較'!='怎麼樣? –

+0

@CaptainGiraffe,我現在已經將它們添加到主帖子中。 End基本上設置了終點。 temp.index被設置爲數組大小,offTheRightEdge被設置爲true。 !=運算符會檢查調用對象的索引和offTheRightEdge值以及傳入對象/兩個比較對象是true還是false。 –

回答

1

它看起來像你的operator !=確實是一個operator ==。在你的調試器中檢查它。

+0

那麼,現在它進入循環,但卡在一個無限循環吐出第12個哈希鍵。哈哈,有趣的是它現在尖叫着我的名字「雷霆隊」。我想知道爲什麼這麼做......嗯。讓我調試,看看我能否找到解決方案,然後再將其標記爲已回答/未解決問題。 –

+0

@JakeMcBride在'operator ++()'中的if裏面似乎也缺少'index ++'。 –

+0

是的,我發現也在調試。我已經標記了你的答案,你的幫助比你知道的要多。它會繼續檢查具有相同索引的for循環,並在for循環的增量部分之前返回。一個簡單的問題是,這總是這樣的,如果你從一個方法中返回,而在該方法內部的for循環中,for循環將無法增加?編輯:更好的措詞,for循環只會增加,如果它擊中for循環的關閉backet},並因此返回意味着它從來沒有擊中那個括號}? –