title: Order of includes with standard libraries and local headers
body: <p>I'm generating bitmap file. The program compiles every time but depending on order of <code>#include</code> at some point, it gives me good or corrupted .bmp file.<br />
  It is from a tutorial and all the file are <a href="https://github.com/caveofprogramming/advanced-cplusplus/tree/master/WritingABitmap/src" rel="nofollow noreferrer">here</a>.</p>
  <p>My structure is as follows:</p>
  <h3>main.cpp</h3>
  <pre><code>#include &lt;iostream&gt;
  #include &quot;Bitmap.h&quot;
  using namespace std;
  int main() {
      Bitmap bitmap(800, 600);
      bitmap.write(&quot;test.bmp&quot;);
  }
  </code></pre>
  <h3>Bitmap.h</h3>
  <pre><code>#include &lt;string&gt;
  #include &lt;cstdint&gt;
  #include &lt;memory&gt;
  using namespace std;
  class Bitmap {
  private:
      int m_width{0};
      int m_height{0};
      unique_ptr&lt;uint8_t[]&gt; m_pPixels{nullptr};
  public:
      Bitmap(int width, int height);
      bool write(string filename);
  };
  </code></pre>
  <h3>BitmapFileHeader.h</h3>
  <pre><code>#include &lt;cstdint&gt;
  using namespace std;
  #pragma pack(2)
  struct BitmapFileHeader {
      char header[2] { 'B', 'M' };
      int32_t fileSize;
      int32_t reserved { 0 };
      int32_t dataOffset;
  };
  </code></pre>
  <h3>BitmapInfoHeader.h</h3>
  <pre><code>#include &lt;cstdint&gt;
  using namespace std;
  #pragma pack(2)
  struct BitmapInfoHeader {
      int32_t headerSize{40};
      int32_t width;
      int32_t height;
      int16_t planes{1};
      int16_t bitsPerPixel{24};
      int32_t compression{0};
      int32_t dataSize{0};
      int32_t horizontalResolution{2400};
      int32_t verticalResolution{2400};
      int32_t colors{0};
      int32_t importantColors{0};
  };
  </code></pre>
  <h3>Bitmap.cpp</h3>
  <pre><code>#include &lt;fstream&gt;
  #include &quot;Bitmap.h&quot;
  #include &quot;BitmapInfoHeader.h&quot;
  #include &quot;BitmapFileHeader.h&quot;
  
  using namespace std;
  
  Bitmap::Bitmap(int width, int height): m_width(width), m_height(height), m_pPixels(new uint8_t[width * height * 3]{ }) {}
  
  bool Bitmap::write(string filename) {
      BitmapFileHeader fileHeader;
      BitmapInfoHeader infoHeader;
  
      fileHeader.fileSize = sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + m_width * m_height * 3;
      fileHeader.dataOffset = sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader);
  
      infoHeader.width = m_width;
      infoHeader.height = m_height;
  
      ofstream file;
      file.open(filename, ios::out | ios::binary);
      file.write((char *)&amp;fileHeader, sizeof(fileHeader));
      file.write((char *)&amp;infoHeader, sizeof(infoHeader));
      file.write((char *)m_pPixels.get(), m_width*m_height*3);
      file.close();
      return true;
  }
  </code></pre>
  <p>Now when in <code>Bitmap.cpp</code> it's in this order everything works fine. When I change the order so that <code>#include &lt;fstream&gt;</code> is last, it still compiles with no errors but generates corrupted file.<br />
  <code>BitmapInfoHeader.h</code> and <code>BitmapInfoHeader.h</code> are just holding a struct each with some <code>int32_t</code> variables in it and don't use <code>fstream</code> at all.<br />
  Why it is the case?</p>