如何使用包含策略类型的容器创建容器迭代器?

时间:2011-02-15 19:13:41

标签: c++ policy

我有以下代码:

#include <map>
#include <string>

class policy1
{
  public:
    struct data
    {
    };
};

template<typename policy>
class policy_user : public policy
{
  typedef std::map<std::string, typename policy::data> mymap;        // good 
  typedef std::map<std::string, 
           typename policy::data >::iterator myiterator;              // bad
  typedef mymap::iterator myseconditerator;                      // also bad
};

失败了:

der.cpp:17: error: type ‘std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, typename policy::data, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, typename policy::data> > >’ is not derived from type ‘policy_user<policy>’
der.cpp:17: error: expected ‘;’ before ‘myiterator’
der.cpp:18: error: type ‘std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, typename policy::data, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, typename policy::data> > >’ is not derived from type ‘policy_user<policy>’
der.cpp:18: error: expected ‘;’ before ‘myseconditerator’

我不知道为什么。有什么帮助我可以完成我尝试做的事情(创建一个迭代器)?

1 个答案:

答案 0 :(得分:7)

您忘记了typedef中的typename

写:

typedef typename std::map<std::string, typename policy::data >::iterator myiterator;      
typedef typename mymap::iterator myseconditerator; 

现在。没关系。 typename是必需的,因为iteratordependent name。另请参阅this