嵌套类不命名类型

时间:2013-11-11 02:22:31

标签: c++ class friend nested-class

在g ++ 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

这是继承问题吗?我试图在main函数中调用iterator,就像通过List :: Iterator

的std :: list一样

1 个答案:

答案 0 :(得分: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;
}