Search code examples
c++c++11foreachhashtableauto

C++11 Array based Hash: Auto not looping


Note this is homework, but we are allowed and encouraged to seek out help as our single professor doesn't have time to respond to all student. If you would not like to help due to the homework nature of this question, please just refrain from responding rather than marking down for asking for help. I don't want my homework done for me. I just want help understanding where my bug lie. All help is appreciated!

I am working on an array based hash table. I have some values in the hash and I want to check the sum of the length of all the elements in the hash. The elements in the hash are strings.

I am using the follow code to iterate through each member that has a value in the hash...

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

The issue is that at the code never loops-not once. It never hits the countOfItems += iter.length();

I debugged the issue and stepped into my iterator the best I could but am still lost. I will post the iterator here these here...

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

When the code reaches the overloaded ++ operator it goes to this other class...

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

Now as I debug and step through the code, it properly gets to the overloaded ++ operator and then finds the first index where there is a value stored then returns that arrayHashIterator object to begin() which in turn returns it back. I would expect that it would then have something to enter the (Auto iter: Hash) loop with but it doesn't.

I do have an overloaded * operator for the arrayHashIterator class as stated below...

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

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

I am almost POSITIVE I have entered elements into my hash correctly because if I open my array for values as well as keys and status in the debugger I find that all the information is there in the correct form in the correct spot.

I am just at a loss for why the (auto iter : hash) would fail to loop. I really do believe the issue is in my overloaded ++ or overloaded * operators, but I cannot be so sure.

A second pair of eyes on this issue would be appreciated. I do not want some instant answer piece of code that works, I would just appreciate some help on finding the bug and how I might tackle resolving it!

EDIT: There is a lot more code to the hash table and the checks for each use case, but I wanted to post specific portions to where my problem is. I can expound on the code provided as requested.

EDIT: Here is what I have for my end() method as well as my overloaded != operator...

UPDATED: Overloaded !=

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

end()

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

Solution

  • It looks like your operator != is really a operator ==. Check it in your debugger.