title: How do you place an Object inside a Room in a text-based game?
body: <p>I'm a coding newbie (and despite what my user name may imply I am far from a pro), and I'm trying to write my own text-based adventure game. I have two questions.</p>
  
  <p>First, I want to implement an Object class. These Objects have names and descriptions and can be placed in rooms, as well as picked up and carried around by the player. What's messing me up is that these Objects are supposed to know what room they were originally in, their "homeroom" so to speak.</p>
  
  <p>I'm not sure how to let each Room know that they have Objects placed within them. Everything that I've tried to do has failed to compile.</p>
  
  <p>I've tried to include Room r as a private variable in Object.cpp and include Room to the Object constructor.</p>
  
  <pre><code>Object::Object(string name, string description, Room *r)
  {
      name_ = name; 
      description_ = description; 
      r_ = r; //assume r_ is a private variable
  
  }
  </code></pre>
  
  <p>Secondly, regarding pointers... This assignment specifies that I must have a vector of pointers of Objects. Would it look like this?</p>
  
  <pre><code>vector&lt;Object*&gt;objectsInRoom; 
  </code></pre>
  
  <p>In main.cpp, I also need a vector of Objects. Is the vector inside the Room class keeping track of Objects in each Room? And is the vector in main.cpp keeping track of all the objects the player carries. Why must the room class have a vector of pointer of Objects? Would not having a vector of Objects suffice?</p>
  
  <p>(I apologize if this sounds vague; this game is based off of an assignment that can be found <a href="https://www.cs.drexel.edu/~mcs172/Sp02/Assignments/HW6/index.html" rel="nofollow noreferrer">here</a>. If you scroll down to the "Extra Credit" portion and go to the first paragraph block marked 10 points, you'll find a much more extensive explanation that I tried to condense above.)</p>
  
  <p>room.cpp</p>
  
  <pre><code>// Room.cpp: implementation of the Room class.
  //
  //////////////////////////////////////////////////////////////////////
  
  #include "Room.h"
  
  
  //////////////////////////////////////////////////////////////////////
  // Construction/Destruction
  //////////////////////////////////////////////////////////////////////
  
  Room::Room()
  {
      name_ = "The void";
      description_ = "There is nothing but blackness in every direction.";
      int i;
      for(i = 0; i &lt; 4; i++) // set all exits to "closed"
          exits_.push_back(NULL);
  
  
  }
  
  Room::Room(string name, string desc)
  {
      name_ = name;
      description_ = desc;
      int i;
      for(i = 0; i &lt; 4; i++) // set all exits to "closed"
          exits_.push_back(NULL);
  
  
  }
  
  Room::~Room()
  {
      cout &lt;&lt; "Destroying: " &lt;&lt; name_ &lt;&lt; endl; 
      // make sure all exits to this room are
      // destroyed so that no one can try to enter
      // this room from another location
      if(exits_[NORTH] != NULL)
          disconnect(NORTH);
      if(exits_[EAST] != NULL)
          disconnect(EAST);
      if(exits_[SOUTH] != NULL)
          disconnect(SOUTH);
      if(exits_[WEST] != NULL)
          disconnect(WEST);
  }
  
  // --- inspectors ---
  Room * Room::north() const
  {
      return exits_[NORTH];
  }
  
  Room * Room::south() const
  {
      return exits_[SOUTH];
  }
  
  Room * Room::east() const
  {
      return exits_[EAST];
  }
  
  Room * Room::west() const
  {
      return exits_[WEST];
  }
  
  
  string Room::name() const
  {
      return name_;
  }
  
  string Room::description() const
  {
      return description_;
  }
  
  /*
  vector&lt;Object&gt; Room::object() const
  {
      return roomObjects; 
  }
  */
  
  // --- mutators ---
  void Room::set_name(string n)
  {
      name_ = n;
  }
  
  void Room::set_description(string d)
  {
      description_ = d;
  }
  
  /*
  void Room::set_object(Object o)
  {
      allObjects.push_back(o); 
  }
  */
  // --- facilitators ---
  bool Room::connect(Direction exit, Room *r, Direction to)
  {
      // check that both exits are free
      if (exits_[exit] != NULL or r-&gt;exits_[to] != NULL)
          return false;
      // make connection
      exits_[exit] = r;
      r-&gt;exits_[to] = this;
      return true;
  }
  
  // --- private methods ---
  
  void Room::disconnect(Direction d)
  {
      // disconnects ALL exits from another
      // room to this one.  It's sloppy, but
      // that's OK.
      Room * other_room;
      other_room = exits_[d];
      int i;
      for(i = 0; i &lt; 4; i++)  {
          if (other_room-&gt;exits_[i] == this)
              other_room-&gt;exits_[i] = NULL;
      }
  }
  
  // --- operators ---
  
  ostream &amp; operator&lt;&lt;(ostream &amp; out, const Room &amp; r) {
      out &lt;&lt; r.name() &lt;&lt; endl;
      out &lt;&lt; r.description() &lt;&lt; endl;
      return out;
  }
  </code></pre>
  
  <p>Object.cpp</p>
  
  <pre><code>#include "Object.h"; 
  
  Object::Object()
  {
      name_ = "Object"; 
      description_ = "The object lies in the room"; 
  
  }
  
  Object::Object(string name, string description)
  {
      name_ = name; 
      description_ = description; 
  
  
  }
  </code></pre>
  
  <p><strong>EDIT</strong>: So, If I simply add    <code>vector&lt;Object*&gt; allObjects</code> under private attributes in Room.h, I get these <a href="https://i.stack.imgur.com/Px8eA.png" rel="nofollow noreferrer">enter image description here</a>. Sorry, I'm not allowed to embed images yet. </p>