Thread: Accessing Iterator as private member of a class

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    34

    Accessing Iterator as private member of a class

    My question is how can I access an iterator that is a private member of a class?

    Code:
    class Sequence
    {
      friend ostream& operator<<(ostream& OUTPUT, const Sequence & s);
      
      public:
       
        ...
    
      private:
      list <int> intList;
      list <int> :: iterator ListIt;
    }
    
    ...
    
    ostream& operator<<(ostream& OUTPUT, const Sequence&  s)
    {
      for(ListIt=s.intList.begin(); ListIt != intList.end(); ListIt++)
     {
      cout << *ListIt;
     }
     return OUTPUT;
    }
    I have tried the above, and several other ways and I keep getting a compilier error that ListIt is undeclared. Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use Sequence::ListIt instead of ListIt. Oh, and you need to name your iterator in the loop and dereference it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And use ++it instead of it++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Whilst including an iterator in a class may be a good idea at some times, it serves no purpose in this instance - class members should be "the minimum necessary to get the work done".

    Having unnecessary class members causes two problems:
    1. class bloat - each class instance will take up extra space everywhere it's being stored, copied, etc.
    2. It "dirties the class", meaning that the class implementation is no longer "clean" or "neat". You are mixing in stuff that is "related, but not part of the object you are dealing with".

    Unless I've completely misunderstood something, you should be able to use "ListIt" as a local variable inside your operator<< function, and there's no need to have it inside the class.

    A case where you may need to use this would be if you for some reason need to walk the list of elements as one step at a time, e.g:
    Code:
    class Sequence
    {
      public:
    ...
      int getFirstElement() { ListIt = s.intList.begin(); return *ListIt; };
      int getNextElement() { if (++ListIt == s.intList.end() return 0; else return *ListIt; };
    ...
      private:
      list <int> intList;
      list <int> :: iterator ListIt;
    };

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 08:42 AM
  2. Replies: 3
    Last Post: 04-10-2009, 02:20 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Replies: 6
    Last Post: 04-27-2006, 10:55 AM
  5. Accessing private data members
    By maloy in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2002, 02:48 PM