title: Segmentation fault while iterating a std::set<std::string> in C++
body: <p>This portion of my code (for <a href="https://softwareengineering.stackexchange.com/questions/115576/designing-subclasses-for-an-abstract-base-class-in-c">this project</a>) gives me a segmentation fault. The source code is available <a href="https://github.com/nikhilbhardwaj/OOPS-Project/tree/package_manager" rel="nofollow noreferrer">here</a>.</p>
  
  <pre><code>void PackageManager::install_package(string pname)
  {
    if(repository-&gt;exists_package(pname)) {
      Package *pkg;
      ConcretePackage *cpkg;
      MetaPackage *mpkg;
      if(repository-&gt;is_virtual(pname)) {
        //code for dealing with meta packages
        mpkg = new MetaPackage(pname);
        pkg = mpkg;
        system-&gt;operator+(pname);
      } else {
        //code for dealing with concrete packages
        cpkg = new ConcretePackage(pname);
        pkg = cpkg;
        system-&gt;operator+(pname);
        if( cpkg-&gt;getDependencies().size() &gt; 0) {
          for(set&lt;string&gt;::iterator sit = pkg-&gt;getDependencies().begin();
              sit!=pkg-&gt;getDependencies().end(); ++sit) {
            cout&lt;&lt;*sit&lt;&lt;endl;
            system-&gt;operator+(*sit);
          }
        }
      }
    } else {
      cout&lt;&lt;"Invalid Package Name"&lt;&lt;endl;
    }
  }
  </code></pre>
  
  <p>Here is the error when I run gdb and also the backtrace.</p>
  
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  0x00007ffff7b6db03 in std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::operator&lt;&lt; &lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) ()
     from /usr/lib/libstdc++.so.6
  (gdb) backtrace
  #0  0x00007ffff7b6db03 in std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::operator&lt;&lt; &lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) ()
     from /usr/lib/libstdc++.so.6
  #1  0x00000000004052e8 in PackageManager::install_package (this=0x7fffffffe280, pname=...) at packagemanager.cpp:39
  #2  0x000000000040575a in main () at packagemanager.cpp:79
  </code></pre>
  
  <p>I'm trying to iterate through a set and perform some operation. I can pode more code if needed. 
  I would also like it if someone could guide me to a place where I could learn to understand these segfaults. I don't know much about them and I tend to panic when I encounter these.</p>
  
  <p>This is the operator+ for the System class.</p>
  
  <pre><code>void System::operator+(string pname)
  {
    installed_packages.insert(pname);
    log.push_back("Added " + pname);
  }
  </code></pre>
  
  <p>I know the design isn't the best but I'm trying to implement the items of a checklist for this project, which covers various areas of Object Oriented Programming. The checklist is also available on github.</p>
  
  <p>I have tried to run the code through the debugger, printng out *sit. It works for sometime and then crashes. I don't know too much about gdb.</p>