0

我一直在尝试编译下面的代码,但显示错误。我不确定它期望的模板名称。我对此很陌生,这是一个非常古老的代码,正在新的 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 */
4

1 回答 1

0

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

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

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

  • 您的类也必须满足InputIterator 概念

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

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

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

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

阅读有关迭代器库的文档。它对迭代器、它的设计和它的概念提供了很好的解释。

于 2013-09-17T09:25:50.550 回答