0

Getting an error in g++ List.cc:19:1: error: âIteratorâ does not name a type when . Header is the following

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);

};

Line 19 is:

Iterator& List::begin() const

Is this an inheritance issue? I am trying to call iterator in the main function just like a std::list via List::Iterator

CChiste
  • 131
  • 1
  • 2
  • 13
  • 1
    Well its not ... C is a member of a though... – Goz Nov 11 '13 at 02:24
  • `B` is a friend of `A`, meaning it can access the private/protected data-members of that class. It doesn't mean it is in itself apart of `A` for instance like `C`. – David G Nov 11 '13 at 02:35
  • "Resembles", "Along the lines of" not a great way to start the question. Be exact. Copy/Paste things into the question. Show the error point and all types. – Martin York Nov 11 '13 at 02:43
  • Alright, so I move the definition of class B inside class A and remove the friend keyword. Now it tells me class B does not name a type... – CChiste Nov 11 '13 at 02:44
  • You write `List::begin` to refer to a member of `List` named `begin`. Now, `Iterator` is also a member of `List`... – n. m. could be an AI Nov 11 '13 at 03:48
  • I see what you mean. I just want to implement Iterator the same way as it's used with std containers. Can anyone tell me how to do this? ie. List::Iterator – CChiste Nov 11 '13 at 04:12
  • 2
    No, because class `Iterator` is an inner class, so you must use like this `List::Iterator`. – BlackMamba Nov 11 '13 at 05:04

1 Answers1

0

This seems to compile just fine for me on MinGW with g++ 4.7.2. Check to ensure that you're qualifying List::Iterator correctly.

#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;
}
inetknght
  • 4,300
  • 1
  • 26
  • 52