Thread: Container - Iterator ++it

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    Container - Iterator ++it

    Hello

    I am trying to create a container. But I have a problem with my iterator.
    I can print everything out using a for statement it->getNext(). So I know my container/iterator works.

    But when I am using ++it I get a segfault. (bold)
    I know my operator++ is wrong but I dont know how to make it work.

    Can someone help me?

    Thank you

    Code:
    #include <iostream>
    using namespace std;
    #include <string>;
    
    class Artikel{
    
      public:
      Artikel(){}
        Artikel(string m_naam, float m_prijs)
          :naam(m_naam), prijs(m_prijs){}
    
        Artikel(const Artikel &other)
        {
          naam = other.naam;
          prijs = other.prijs;
        }
    
        string getNaam()
        {
          return naam;
        }
    
        float getPrijs()
        {
          return prijs;
        }
    
    
        friend ostream& operator<<(ostream &out, const Artikel &k)
        {
         out << k.naam << " " << k.prijs << endl;
          return out;
        }
      private:
        string naam;
        float prijs;
    
    };
    
    class Iterator{
    
      public:
        Iterator(Artikel *m_current = NULL, Iterator *m_next = NULL){
          next = m_next;
          if(m_current != NULL)
    	current = new Artikel(*m_current);
          else
    	current = NULL;
        }
    
        Iterator(const Iterator&other)
        {
          cout << "copy iterator\n";
          current = new Artikel(*other.current);
          next = other.next;
    
        }
    
        Iterator* getNext()
        {
          return next;
        }
    
        Artikel& getArtikel()
        {
          return *current;
        }
    
        void setNext(Iterator *it)
        {
          next = it;
        }
    
        bool operator!=(const Iterator& other)
        {
          if(current == other.current)
            return false;
          else
            return true;
        }
    
        friend ostream& operator<<(ostream &out, const Iterator&k)
        {
          out << k.current->getNaam() << k.current->getPrijs() << endl;
          return out;
        }
    
        Iterator& operator++()
        {
          *this = *next;
          this->current = next->current;
          return *this;
        }
    
    
    
      private:
        Iterator *next;
        Artikel *current;
    
    };
    
    class Container{
    
      public:
    
        Iterator* getTail()
        {
          return tail;
        }
    
        Iterator* getHead(){
          return head;
        }
        void Add(Artikel *a);
        Container(){
          head = NULL;
          tail = new Iterator();
        }
      private:
    
        Iterator *tail, *head;
    
    
    };
    
    void Container::Add(Artikel *a)
    {
    
      if(head == NULL)
      {
        head = new Iterator(a, tail);
      } else {
    
        Iterator *it = head;
    
        while(it->getNext() != tail)
        {
            it = it->getNext();
        }
    
        Iterator *add = new Iterator(a, tail);
        it->setNext(add);
    
    
      }
    
    
    }
    
    
    
    int main(void){
    
      Artikel a1("Karskall ", 12.45);
      Artikel a2("Alfa-beta ", 1.12);
      Artikel a3("dijkstra ", 45.23);
      Artikel a4("Floyd ", 1003.34);
    
      Container c;
    
      c.Add(&a1);
      c.Add(&a2);
      c.Add(&a3);
      c.Add(&a4);
    
      Iterator *it;
    /* WORKS
      for(it = c.getHead(); it != c.getTail(); it = it->getNext())
      {
        cout << *it;
      }
    */
      for(it = c.getHead(); it != c.getTail(); ++it)
      {
        cout << *it;
      }
    
    
      return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "this" isn't really a modifiable kind of thing. Presumably you want to change "current" instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iterator to unspecified container?
    By StainedBlue in forum C++ Programming
    Replies: 16
    Last Post: 06-04-2010, 05:02 AM
  2. sorting container
    By l2u in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2007, 01:12 PM
  3. stl container to store more than 1 type
    By sujeet1 in forum C++ Programming
    Replies: 7
    Last Post: 05-09-2007, 04:10 AM
  4. Replies: 1
    Last Post: 01-23-2006, 07:12 PM
  5. Linked List Queue Implementation help
    By Kenogu Labz in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:14 AM