简体   繁体   中英

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!

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM