title: C++ primer 5 edition: class Message and Folder
body: <p>I am trying to write the solution for an exercise in C++ primer 5 ed. The program is writing two classes <code>Message</code> and <code>Folder</code>. each Folder has a set of pointers to a Message object and each Message has a set of pointers to Folders in which it exists so I wrote this code:</p>
  
  <ul>
  <li><p>For brevity I didn't show the whole code so I'll try to focus on the part may causing problem:</p>
  
  <pre><code>class Folder;
  class Message
  {
  public:
      Message(const std::string &amp; = "");
      Message(const Message&amp;);
      Message(Message&amp;&amp;);
      Message&amp; operator=(const Message&amp;);
      Message&amp; operator=(Message&amp;&amp;);
      ~Message();
  
      const std::string&amp; getMsg()const;
      void setMsg(const std::string&amp;);
      void  save(Folder&amp;);
      void remove(Folder&amp;);
  
  //private:
      std::string msg_;
      std::set&lt;Folder*&gt; dirs_;
  
      void addToDirs(const Message&amp;);
      void delFromDirs();
      void moveMsgs(Message&amp;);
  
      friend void swap(Message&amp;, Message&amp;);
      friend class Folder;
  };
  
  class Folder
  {
  public:
      void addMsg(Message&amp;);
      void delMsg(Message&amp;);
  
  //private:
      std::set&lt;Message*&gt; msgs_;
  };
  
  Message::Message(const std::string&amp; msg) : 
      msg_(msg)
  {}
  
  Message::Message(const Message&amp; rhs) :
      msg_(rhs.msg_), 
      dirs_(rhs.dirs_)
  {
      addToDirs(rhs);
  }
  
  Message&amp; Message::operator=(const Message&amp; rhs)
  {
      delFromDirs();
      msg_ = rhs.msg_;
      dirs_ = rhs.dirs_;
      addToDirs(rhs);
  
      return *this;
  }
  
  
  void Message::addToDirs(const Message&amp; msg)
  {
      for (auto&amp; fdr : msg.dirs_)
          fdr-&gt;addMsg(*this);
  }
  
  Message::~Message()
  {
      delFromDirs();
  }
  
  void Message::save(Folder&amp; fdr)
  {
      fdr.addMsg(*this);
  }
  
  void Message::remove(Folder&amp; fdr)
  {
      fdr.delMsg(*this);
  }
  
  void Message::delFromDirs()
  {
      for (auto fdr : dirs_)
          fdr-&gt;delMsg(*this);
  }
  
  void Folder::addMsg( Message&amp; msg)
  {
      msgs_.insert(&amp;msg);
      msg.dirs_.insert(this);
  }
  
  void Folder::delMsg(Message&amp; msg)
  {
      msgs_.erase(&amp;msg);
      msg.dirs_.erase(this);
  }
  
  const std::string&amp; Message::getMsg()const
  {
      return msg_;
  }
  
  void Message::setMsg(const std::string&amp; msg)
  {
      msg_ = msg;
  }
  
  void Message::moveMsgs(Message&amp; msg)
  {
      msg_ = std::move(msg.msg_);
      dirs_ = std::move(msg.dirs_);
  
      for (auto&amp; fdr : dirs_)
      {
          fdr-&gt;delMsg(msg);
          fdr-&gt;addMsg(*this);
      }
      msg.dirs_.clear();
  }
  
  Message::Message(Message&amp;&amp; rhs) :
      msg_(std::move(rhs.msg_)),
      dirs_(std::move(rhs.dirs_))
  {
      std::cout &lt;&lt; "Message's move-ctor\n";
      moveMsgs(rhs);
  }
  Message&amp; Message::operator=(Message&amp;&amp; rhs)
  {
      std::cout &lt;&lt; "=(Message&amp;&amp;)\n";
      if (this != &amp;rhs) 
      {
          delFromDirs();
          moveMsgs(rhs);
      }
      return *this;
  }
  
  
  
  int main()
  {
  
      Folder f1, f2, f3;
  
      Message m1("Message m1"),
          m2("Message m2"), m3("Message m3"), m4("Message m4"),
          m5("Message m5");
  
      f1.addMsg(m1);
      f1.addMsg(m2);
      f1.addMsg(m5);
      f2.addMsg(m1);
      f2.addMsg(m4);
      f3.addMsg(m3);
  
  
      cout &lt;&lt; "f1.msgs: \n";
      for (auto&amp; msg : f1.msgs_)
          cout &lt;&lt; msg-&gt;msg_ &lt;&lt; endl;
      cout &lt;&lt; endl;
  
  
  
  
      std::cout &lt;&lt; "\nDone!\n";
  }
  </code></pre></li>
  <li><p>The problem is: The code works fine on VC++19 and compiled on wandbox.org using GCC-Head 10 and Clang 10 and works fine but I have an another compiler on my phone called "coding C++" it says:</p></li>
  </ul>
  
  <p>"process completed (signal 11) press enter".</p>
  
  <ul>
  <li>I want to know whether <code>delFromDirs()</code> is correctly handling deletion? Because the destructor of Message should remove each folder from its set of folders and each folder from them should remove that message from its set of messages.</li>
  </ul>