Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;操作员++;模板泛型树类的模板迭代器重载 我开发了一个C++树模板类,我想编写一组自定义迭代器。_C++_Templates_Tree_Iterator - Fatal编程技术网

C++;操作员++;模板泛型树类的模板迭代器重载 我开发了一个C++树模板类,我想编写一组自定义迭代器。

C++;操作员++;模板泛型树类的模板迭代器重载 我开发了一个C++树模板类,我想编写一组自定义迭代器。,c++,templates,tree,iterator,C++,Templates,Tree,Iterator,GenericTree类迭代器应按如下方式使用: GenericTree<int> tree; GenericTree<int>::iterator<PREORDER> preorder_iterator; for(preorder_iterator = tree.begin(); preorder_iterator != tree.end(); ++preorder_iterato){ //Do something on the node pointe

GenericTree类迭代器应按如下方式使用:

GenericTree<int> tree;
GenericTree<int>::iterator<PREORDER> preorder_iterator;

for(preorder_iterator = tree.begin(); preorder_iterator != tree.end(); ++preorder_iterato){
  //Do something on the node pointed by the preorder_iterator.
}
GenericTree树;
GenericTree::迭代器前序迭代器;
for(preorder\u iterator=tree.begin();preorder\u iterator!=tree.end();++preorder\u iterato){
//在前序迭代器指向的节点上执行某些操作。
}
在上面的示例中,++preorder\u迭代器应该调用为int和FORWARD类型定义的迭代器类的重载运算符+++()方法,但代码不会编译。我正在使用g++(Ubuntu 4.8.2-19ubuntu1)4.8.2

下面是迭代器类的代码。为了缩短示例,我用向量替换了GenericTree类,并且完全省略了begin()方法的源代码

#include <iostream>
#include <string>
#include <vector>

using namespace std;

template<typename P> class GenericTree{
        public:
                vector<P> data;
                GenericTree() {}
                ~GenericTree(){}

        class PREORDER{
        };

        class POSTORDER{
        };

        // ----------------------------------------------------------------------------------
        template<typename IT> class my_iterator : public std::iterator<std::forward_iterator_tag, P> {
                private:
                        int index;
                protected:
                public:
                        my_iterator()           { index=0; }
                        ~my_iterator()  {}

                IT& operator++();
        };
// ----------------------------------------------------------------------------------
        template<> my_iterator<PREORDER>& my_iterator<PREORDER>::operator++(){
                cout << "Preorder visit of GenericTree of nodes containing int values." << endl;
                return (*this);
        }

        template<> my_iterator<POSTORDER>& my_iterator<POSTORDER>::operator++(){
                cout << "Postorder visit of GenericTree of nodes containing int values." << endl;
                return (*this);
        }
};


int main(int argc, char** argv){
        GenericTree<int> c_int;
        GenericTree<int>::my_iterator<GenericTree<int>::PREORDER> iterator_int;

        for(iterator_int = c_int.begin(); iterator_int != c_int.end(); ++iterator_int)
                ;

        GenericTree<string> c_string;
        GenericTree<string>::my_iterator<GenericTree<string>::POSTORDER> iterator_str;

        for(iterator_str = c_string.begin(); iterator_str != c_string.end(); ++iterator__str)
                ;


}
#包括
#包括
#包括
使用名称空间std;
模板类GenericTree{
公众:
向量

数据; GenericTree(){} ~GenericTree(){} 类前序{ }; 班级邮购{ }; // ---------------------------------------------------------------------------------- 模板类my_迭代器:public std::iterator{ 私人: 整数指数; 受保护的: 公众: my_迭代器(){index=0;} ~my_迭代器(){} IT&operator++(); }; // ---------------------------------------------------------------------------------- 模板my_iterator&my_iterator::operator++(){


cout将迭代器类移到
GenericTree之外
部分专门化
结构,我的迭代器
完成这项工作。
以下内容可能会有所帮助:

namespace internal
{
    class PREORDER {};
    class POSTORDER {};

    // ------------------------------------------------------------------------------
    template<typename P, typename IT>
    class my_iterator;


    template<typename P>
    class my_iterator<P, PREORDER>
    : public std::iterator<std::forward_iterator_tag, P>
    {
    private:
        int index;
    protected:
    public:
        my_iterator () { index = 0; }

        my_iterator& operator++ () {
            std::cout << "Preorder visit of GenericTree of nodes containing int values." << std::endl;
            return (*this);
        }
    };
    // ------------------------------------------------------------------------------
    template<typename P>
    class my_iterator<P, POSTORDER>
    : public std::iterator<std::forward_iterator_tag, P>
    {
    private:
        int index;
    protected:
    public:
        my_iterator () { index = 0; }

        my_iterator& operator++ () {
            std::cout << "Postorder visit of GenericTree of nodes containing int values." << std::endl;
            return (*this);
        }
    };

}

template<typename P> class GenericTree
{
public:
    template <typename IT>
    using my_iterator = internal::my_iterator<P, IT>;
    using PREORDER = internal::PREORDER;
    using POSTORDER = internal::POSTORDER;

    std::vector<P> data;
    GenericTree () {}
    ~GenericTree () {}
};
名称空间内部
{
类前序{};
类后序{};
// ------------------------------------------------------------------------------
样板
类my_迭代器;
样板
类my_迭代器
:public std::迭代器
{
私人:
整数指数;
受保护的:
公众:
my_迭代器(){index=0;}
my_迭代器和运算符++(){

std::无法将类的声明/定义
my_iterator
移动到
GenericTree
之外。错误确切地说明了发生了什么,专门化不能在类范围内。可能与有关,但如果我在实例化迭代器以处理树时将迭代器类移出GenericTree类,我应该通过树指针/对迭代器的引用,以便能够遍历树结构。如果我设法将iteraror类保留在GenericTree类中,那么我认为使用这两个类编写的代码是明确的。如果两个Cals保持嵌套,您认为问题可以解决吗?我不确定是否理解您的第二个注释,但我提供了
typedef
GenericTree
中允许相同的接口。确实,在定义类
GenericTree
之后,您可能必须实现
my_迭代器的一些方法。我可能在这里错了,但要使用my_迭代器的实现,我应该编写如下内容:GenericTree\u int;my_迭代器iter\u int(&tree)。也就是说,我应该以某种方式向iter_int传递一个指向它应该迭代的树的referec指针。如果添加一个伪类型以保持内部类部分专用化,则似乎可以保持类嵌套。
namespace internal
{
    class PREORDER {};
    class POSTORDER {};

    // ------------------------------------------------------------------------------
    template<typename P, typename IT>
    class my_iterator;


    template<typename P>
    class my_iterator<P, PREORDER>
    : public std::iterator<std::forward_iterator_tag, P>
    {
    private:
        int index;
    protected:
    public:
        my_iterator () { index = 0; }

        my_iterator& operator++ () {
            std::cout << "Preorder visit of GenericTree of nodes containing int values." << std::endl;
            return (*this);
        }
    };
    // ------------------------------------------------------------------------------
    template<typename P>
    class my_iterator<P, POSTORDER>
    : public std::iterator<std::forward_iterator_tag, P>
    {
    private:
        int index;
    protected:
    public:
        my_iterator () { index = 0; }

        my_iterator& operator++ () {
            std::cout << "Postorder visit of GenericTree of nodes containing int values." << std::endl;
            return (*this);
        }
    };

}

template<typename P> class GenericTree
{
public:
    template <typename IT>
    using my_iterator = internal::my_iterator<P, IT>;
    using PREORDER = internal::PREORDER;
    using POSTORDER = internal::POSTORDER;

    std::vector<P> data;
    GenericTree () {}
    ~GenericTree () {}
};