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

发布于 2024-10-17 07:26:04 字数 1638 浏览 4 评论 0原文

我有以下代码:

#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’

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

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 help how I can accomplish what I try to do (create an iterator)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够 2024-10-24 07:26:04

您忘记了 typedef 中的 typename

写:

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

现在。没关系。 typename 是必需的,因为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文