开发者

How can I create a container iterator with the container holding a policy type?

I have the following code:

#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
};

that fails with:

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’

And I have no clue why. Any hel开发者_开发百科p how I can accomplish what I try to do (create an iterator)?


You forgot typename in typedefs.

Write:

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

Now. It's fine. typename is required because iterator is a dependent name. See also this

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜