title: Correct way to use 1 variable in all other files
body: <p>I am making game in C++. I'm using SFML. I have game.h and game.cpp files which are connected and share/use variables.</p>
  
  <p>I want to use some of those variables in other files. I tried example:</p>
  
  <pre><code>extern int variable; 
  </code></pre>
  
  <p>And it is working fine. Problem is when i try to make extern this:</p>
  
  <pre><code>sf::Texture hoverTexture;
  std::vector&lt;sf::Sprite&gt; Hover_Tiles;
  sf::Sprite hover;
  </code></pre>
  
  <p>And use those sprites/textures in other cpp file. Just trying to break my code into few files to make it easier to read.
  So my question would be is it possible to make those mentioned sprites/textures global to use them in other files correctly. What is the best way to do it. If it isn't possible what should I do and how? </p>
  
  <p>How to use those sprites/textures in other files. How to connect them?
  At least point me to somewhere. Thanks.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>After all includes on top of game.h</p>
  
  <pre><code>extern std::vector&lt;sf::Sprite&gt; Tiles;
  extern sf::Sprite tile;
  
  extern sf::Texture seaTexture;
  extern sf::Texture grassTexture;
  extern sf::Texture rockTexture;
  extern sf::Texture forestTexture;
  
  extern sf::Texture hoverTexture;
  extern std::vector&lt;sf::Sprite&gt; Hover_Tiles;
  extern sf::Sprite hover;
  
  extern sf::Texture hoverTexture2;
  extern std::vector&lt;sf::Sprite&gt; Hover_Tiles2;
  extern sf::Sprite hover2;
  
  extern sf::Texture houseTexture;
  extern std::vector&lt;sf::Sprite&gt; House_Tiles;
  extern sf::Sprite house;
  
  extern int map[20][10];
  </code></pre>
  
  <p>In other file map.cpp</p>
  
  <pre><code>#include "game.h"
  #include "map.h"
  
  #include "SFML\Graphics.hpp"
  #include &lt;vector&gt;
  
  std::vector&lt;sf::Sprite&gt; Tiles;
  sf::Sprite tile;
  
  sf::Texture seaTexture;
  sf::Texture grassTexture;
  sf::Texture rockTexture;
  sf::Texture forestTexture;
  
  sf::Texture hoverTexture;
  std::vector&lt;sf::Sprite&gt; Hover_Tiles;
  sf::Sprite hover;
  
  sf::Texture hoverTexture2;
  std::vector&lt;sf::Sprite&gt; Hover_Tiles2;
  sf::Sprite hover2;
  
  sf::Texture houseTexture;
  std::vector&lt;sf::Sprite&gt; House_Tiles;
  sf::Sprite house;
  
  
  int map[20][10] =
  {
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
      { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 }
  };
  </code></pre>
  
  <p>In the same map.cpp I made void function which is first defined in map.h. That function holds some code that works great in game.cpp (if this code I posted up is in game.h). I also have problem with Exception thrown:</p>
  
  <pre><code>Exception thrown at 0x7749B2E3 (ntdll.dll) in App.exe: 0xC0000005:
  Access violation writing location 0x00000004.
  </code></pre>
  
  <p>At map.cpp line:</p>
  
  <pre><code>sf::Texture seaTexture;
  </code></pre>
  
  <p>I hope this code I posted helps in fixing problem. When I transfer those sprites/textures/variables into other file using extern it won't work but everything in one (game.h and game.cpp) works great.</p>