title: Reference C++ struct object in another file?
body: <p>I'm in the process of trying to make a game-in-progress more modular. I'd like to be able to declare a single array of all the room_t objects in the game (room_t rooms[]), store it in world.cpp and call it from other files.</p>
  
  <p>The truncated code below does not work, but it's as far as I've gotten. I think I need to use extern but have not been able to find a method that works correctly. If I try and declare the array in the header file, I get a duplicate object error (as each file calls world.h, I'd assume). </p>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "world.h"
  
  int main()
  {
      int currentLocation = 0;
      cout &lt;&lt; "Room: " &lt;&lt; rooms[currentLocation].name &lt;&lt; "\n";
      // error: 'rooms' was not declared in this scope
      cout &lt;&lt; rooms[currentLocation].desc &lt;&lt; "\n";    
      return 0;
  }
  </code></pre>
  
  <p>world.h</p>
  
  <pre><code>#ifndef WORLD_H
  #define WORLD_H
  #include &lt;string&gt;
  
  
  const int ROOM_EXIT_LIST = 10;
  const int ROOM_INVENTORY_SIZE = 10;
  
  struct room_t
  {
      std::string name;
      std::string desc;
      int exits[ROOM_EXIT_LIST];
      int inventory[ROOM_INVENTORY_SIZE];
  };  
  
  #endif
  </code></pre>
  
  <p>world.cpp</p>
  
  <pre><code>#include "world.h"
  
  room_t rooms[] = {
    {"Bedroom", "There is a bed in here.", {-1,1,2,-1} },
    {"Kitchen", "Knives! Knives everywhere!", {0,-1,3,-1} },
    {"Hallway North", "A long corridor.",{-1,-1,-1,0} },
    {"Hallway South", "A long corridor.",{-1,-1,-1,1} }
  };
  </code></pre>