2017-07-23 63 views
0

我有以下类,我试图从内部类迭代从一个内部类访问外部公有成员

#ifndef __LISTMAP_H__ 
#define __LISTMAP_H__ 

#include "xless.h" 
#include "xpair.h" 

template <typename Key, typename Value, class Less=xless<Key>> 
class listmap { 
    public: 
     using key_type = Key; 
     using mapped_type = Value; 
     using value_type = xpair<const key_type, mapped_type>; 
    private: 
     Less less; 
     struct node; 
     struct link { 
     node* next{}; 
     node* prev{}; 
     link (node* next, node* prev): next(next), prev(prev){} 
     }; 
     struct node: link { 
     value_type value{}; 
     node (node* next, node* prev, const value_type&); 
     }; 
     node* anchor() { return static_cast<node*> (&anchor_); } 
     link anchor_ {anchor(), anchor()}; 
    public: 
     class iterator; 
     listmap(){}; 
     listmap (const listmap&) = default; 
     listmap& operator= (const listmap&) = default; 
     ~listmap(); 
     iterator insert (const value_type&); 
     iterator find (const key_type&); 
     iterator erase (iterator position); 
     iterator begin() { return anchor()->next; } 
     iterator end() { return anchor(); } 
     bool empty() const { return begin() == end(); } 
}; 


template <typename Key, typename Value, class Less> 
class listmap<Key,Value,Less>::iterator { 
    private: 
     friend class listmap<Key,Value>; 
     listmap<Key,Value,Less>::node* where {nullptr}; 
     iterator (node* where): where(where){}; 
    public: 
     iterator(){} 
     value_type& operator*(); 
     value_type* operator->(); 
     iterator& operator++(); //++itor 
     iterator& operator--(); //--itor 
     void erase(); 
     bool operator== (const iterator&) const; 
     bool operator!= (const iterator&) const; 
}; 

template <typename Key, typename Value, class Less> 
value_type& listmap<Key,Value,Less>::iterator<Key,Value,Less>::operator*() 
{ 
     return where->value; 
} 

#include "listmap.tcc" 
#endif 

的问题是,VALUE_TYPE重载运算符*是从类公共成员listmap,它不是静态的,所以我不知道如何完成operator *()的声明。我不想通过更改代码的结构来修复该错误。例如:制作

using value_type = xpair<const key_type, mapped_type>; 

全球。我只是想知道是否有一些其他技巧可以用来访问value_type。

....编辑:我不知道内部类如何识别VALUE_TYPE

+0

好的,那么你会如何做这个声明? –

+0

糟糕,我以为你试图访问一个成员变量或函数,而不是一个类型。 – immibis

回答

0

这是几乎一样的迭代器,你只需要添加typename关键字

typename listmap<Key,Value,Less>::value_type 

static岬没有按”对于某种类型而言,

别名内迭代

template <typename Key, typename Value, class Less> 
class listmap<Key,Value,Less>::iterator { 
    ... 
    using value_type = typename listmap<Key,Value,Less>::value_type; 

}; 

让您更简洁地写定义使用自动后缀类型:

template <typename Key, typename Value, class Less> 
auto listmap<Key,Value,Less>::iterator::operator*() -> value_type& 
{ 
     return where->value; 
} 

小心:内部iterator类是没有模板,只有listmap是:

listmap<Key,Value,Less>::iterator<Key,Value,Less>::operator 
//        ~~~~~~~~~~~~~~~~ remove this 

顺便说一句,别忘了others

相关问题