input_iterator询问模板名称

input_iterator asking for a template name

本文关键字:iterator input      更新时间:2023-10-16

我一直在尝试编译下面的代码,但显示错误。我不确定它期望什么模板名称。我是新手,这是一个非常旧的代码,正在新的g ++编译器上进行编译。谁能帮忙?

提前感谢,欣赏它。

错误:

./dir.h:12: error: expected template-name before â<â token
./dir.h:12: error: expected â{â before â<â token
./dir.h:12: error: expected unqualified-id before â<â token
make: *** exit code 1 making Life.o

法典:

#if !defined(DIRECTORY_H)
#define DIRECTORY_H
#include <string>
#include <algorithm>
#include <iterator>
//using std::input_iterator;
using std::string;
    struct dir_it_rep;
    class dir_it : public input_iterator<string,int>  //<------- Line 12
    {
    public:
      dir_it();                              // "past the end" ctor
      explicit dir_it(string const &);       // the "normal" ctor
      dir_it(dir_it const &it);
      ~dir_it();
      dir_it &operator= (dir_it const &it);
      string operator* () const { return i_value; }
      dir_it &operator++ ();
      dir_it operator++ (int) { dir_it rc (*this); operator++(); return rc; }
      bool operator== (dir_it const &it) const;
      bool operator!= (dir_it const &it) const { return !operator== (it); }
    private:
      dir_it_rep *i_rep;    // representation for the next value
      string     i_value;   // the current value
    };


#endif /* DIRECTORY_H */

第一:没有std::input_iterator。第二:迭代器是通过概念而不是通过类层次结构来分配的。
标准库提供了基类std::iterator,为迭代器提供通用的兼容接口(换句话说,为了简化事情)。但是不同类型的迭代器只是您自己的迭代器实现必须满足的概念才能属于特定的迭代器类别

换句话说:不同的迭代器类别(前向迭代器、输入迭代器、双向迭代器)只是类概念。也就是说,例如,如果要编写一个要被视为前向迭代器的类,则类必须满足条件/功能列表:

  • 您的类必须是默认可构造的。

  • 您的类也必须满足输入迭代器概念。

  • 必须重载满足一组指定行为的前增量和后增量运算符(阅读文档)。

  • 该类必须是可取消引用的,即重载operator*()

以下是解释ForwardIterator概念要求的文档。

此外,标准库提供了一组类,这些类充当"标签"来确定迭代器类的类别(因为迭代器不是类层次结构,我们需要一个间接形式来确定迭代器的类别。请注意,在常见情况下,这并不令人担忧,因为我们以通用方式使用迭代器): http://en.cppreference.com/w/cpp/iterator/iterator_tags

阅读有关迭代器库的文档。它提供了关于迭代器、其分配和概念的良好解释。