Ksenia Spitsin
Ksenia Spitsin

Reputation: 11

C++ iterators : error on implementing an iterator class

I have an assignment where I need to implement a hash table class including an iterator class to it. I've defined the class Hash:

template <class KeyType, class ValueType>
class Hash
{
    class Pair
    {
    public:
        Pair(KeyType a_key) :
        m_key(a_key)
        {
        }
        Pair(KeyType a_key, ValueType a_val) :
        m_key(a_key), m_val(a_val)
        {
        }

        KeyType m_key;
        ValueType m_val;    
    };

public:
    typedef size_t (*HashFunc)(KeyType);

    class Iterator
    {
        friend class Pair;
    public:
        //Iterator()
        //Iterator(const Iterator& a_other)
        //Iterator& operator=(const Iterator& a_other)
        //~Iterator

        void operator++();

        ValueType& operator*() { return *m_itr->m_val;}

    private:
        typedef typename std::list<Pair*>::iterator m_itr;
        size_t m_hashIndex;
    };

    Hash(size_t a_size, HashFunc a_func);
    ~Hash();

private:
    HashFunc m_hashFunc;
    size_t m_size;
    std::list<Pair*>*  m_hash;
};

and in line ValueType& operator*() { return *m_itr->m_val;} I get the following error: expected primary-expression before ‘->’ token and I can't find the problem. Can anyone offer some advice? Thanks in advance!

Upvotes: 1

Views: 39

Answers (1)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122133

This

typedef typename std::list<Pair*>::iterator m_itr;

is a typedef that declares an alias type for std::list<Pair*>::iterator called m_itr. I suppose you wanted to have a member of that type:

typedef typename std::list<Pair*>::iterator itr_type;
itr_type m_iter;

Upvotes: 3

Related Questions