简体   繁体   English

非模板容器迭代器

[英]Non-template container iterators

Let's suppose we have a class Container without a template, and the functions begin() and end() , witch returns a class Container_iterator .假设我们有一个没有模板的类Container ,以及函数begin()end() ,女巫返回一个类Container_iterator The Container class has a map in it: Container类中有一个映射:

class Container
{
    Container_iterator begin();
    Container_iterator end();
};
class Container_iterator
{
    Container operator*();
}

Now, this code does not compile, so we add the forward declerations of the classes.现在,此代码无法编译,因此我们添加了类的前向声明。 However, in the code still does not compile, saying that Container is a incomplete type.但是,在代码中仍然没有编译,说Container是一个不完整的类型。 Is there any way to get the code to compile, or is implementing iterators without templates not possible?有没有办法让代码编译,或者不可能在没有模板的情况下实现迭代器?

Edit: code编辑:代码

namespace json
{
    class JSONobj
    {
        void* to_value;
        json_types t;

    public:
        typedef std::unordered_map<std::string, JSONobj> json_obj;


        // iterator class
        class iterator : std::iterator < std::forward_iterator_tag, JSONobj, size_t>
        {
        public:

        private:
            json_array::iterator it_array;
            json_obj::iterator it_obj;
            json_iterator_types t;

        public:
            iterator(JSONobj::json_array::iterator& _it_array);

            // prefix ++ operator
            iterator& operator++();

            // suffix ++ operator
            iterator& operator++(int);


            std::string key();
            // dereferance operator
            reference operator*();
        };

        inline reference front() {}
        inline reference back() {}
    }
}

You can just make Container_iterator a nested type member of Container , and it'll work:你可以只让Container_iterator的嵌套类型成员Container ,它会工作:

class Container
{
  class Container_iterator
  {
     Container operator*();
  };

  Container_iterator begin();
  Container_iterator end();
};

Also, it seems odd that an iterator would return a Container instead of an element of the Container , but the solution above would work in that case as well.此外,它似乎很奇怪,迭代器将返回一个Container ,而不是一个元素的Container ,但上述解决方案将在这种情况下正常工作。

Is there any way to get the code to compile有没有办法让代码编译

Simply declare the classes before using the declaration.在使用声明之前简单地声明类。 And add the missing semicolon.并添加缺少的分号。

Defining the iterator as nested class as suggested in cigien's answer is conventional and I recommend that approach.按照 cigien 的回答中的建议将迭代器定义为嵌套类是传统的,我推荐这种方法。 But it is not strictly necessary.但这并不是绝对必要的。

However, in the code still does not compile但是,在代码中仍然没有编译

Yes it does .是的

saying that Container is a incomplete type.说 Container 是一个不完整的类型。

This is not something that can be solved with a forward declaration.这不是通过前向声明可以解决的。 This means that you are using the definition of the class before that definition.这意味着您在该定义之前使用该类的定义。

This is an error that is not produced in your example since the definition of Container is not used.这是您的示例中未产生的错误,因为未使用Container的定义。

This error can be fixed by ordering the code differently such that the class is defined before its definition is used.可以通过对代码进行不同的排序来修复此错误,以便在使用其定义之前定义该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM