首页 > 解决方案 > 对地图值进行自定义迭代器时出现 C++ 错误

问题描述

我有这个代码:

template <typename Iter>
class map_iterator : public std::iterator<std::bidirectional_iterator_tag, typename Iter::value_type::second_type> {
public:
    map_iterator() {}
    map_iterator(Iter j) : i(j) {}
    map_iterator& operator++() { ++i; return *this; }
    map_iterator operator++(int) { auto tmp = *this; ++(*this); return tmp; }
    map_iterator& operator--() { --i; return *this; }
    map_iterator operator--(int) { auto tmp = *this; --(*this); return tmp; }
    bool operator==(map_iterator j) const { return i == j.i; }
    bool operator!=(map_iterator j) const { return !(*this == j); }
    reference operator*() { return i->second; }
    pointer operator->() { return &i->second; }
protected:
    Iter i;
};

template <typename Iter>
inline map_iterator<Iter> make_map_iterator(Iter j) { return map_iterator<Iter>(j); }

using route_departure_container = std::map<packed_time, route_departure_o>;

template <typename Iter>
using route_departure_const_iterator = map_iterator;

template <typename Iter>
route_departure_const_iterator<Iter> departure_at(const std::pair<key, const platform_route_o&>& pr, packed_time tm);

我收到编译器错误:语法错误:缺少';' 在 '*' 等之前和 C4430 缺少行上的类型说明符

reference operator*() { return i->second; }
pointer operator->() { return &i->second; }

问题是什么?

标签: c++hashmapiterator

解决方案


推荐阅读