0

List.cc:19:1: error: âIteratorâ does not name a type . 标题如下

class List {
private:
class Element {
 public:
  char data;
  Element *next;
  Element *prev;
  Element(Element *n,Element *p, char d);
};
Element *first;

public:
class Iterator {
 public:
  Iterator();
  void operator++();
  void operator--();
  char& operator*();
  const char& operator*() const;
  bool operator==(const Iterator& itr);
  bool operator!=(const Iterator& itr);
 private:
  Iterator(const Element& ele);
  Iterator *it;
};
List();
~List();
Iterator& begin() const;
Iterator& end() const;
void insert(Iterator itr, char c);
void erase(Iterator itr);

};

第 19 行是:

Iterator& List::begin() const

这是继承问题吗?我试图在主函数中调用迭代器,就像通过 List::Iterator 调用 std::list

4

1 回答 1

0

这似乎对我来说在带有 g++ 4.7.2 的 MinGW 上编译得很好。检查以确保您正确地限定 List::Iterator。

#include <iostream>

class List {
private:
class Element {
 public:
  char data;
  Element *next;
  Element *prev;
  Element(Element *n,Element *p, char d) : next(n), prev(p), data(d){}
};
Element *first;

public:
class Iterator {
 public:
  Iterator(){}
  void operator++(){}
  void operator--(){}
  char& operator*(){return data;}
  const char& operator*() const{return data;}
  bool operator==(const Iterator& itr){return false;}
  bool operator!=(const Iterator& itr){return false;}
 private:
  Iterator(const Element& ele){}
  Iterator *it;
  char data;
};

Iterator it;

List(){}
~List(){}
const Iterator& begin() const{return it;}
const Iterator& end() const{return it;}
void insert(Iterator itr, char c){}
void erase(Iterator itr){}

};

int main(int argc, char**argv)
{
    List blah;
    List::Iterator it = blah.begin();

    return 0;
}
于 2013-11-11T04:19:10.320 回答