title: Visual Studio 2012 Express compiler not linking header files together correctly
body: <p>I am setting up some framework for a little 2D game. Right now, I just have a few classes, but I am immediately falling into compiler problems.</p>
  
  <p>I have run this program with only the <code>main</code> function, so I can confirm that the Allegro (graphics library) library linking has worked.</p>
  
  <p>I have put all my header files (.h) under the "Header Files" section in the Solution explorer and all my source (.cpp) in the "Source Files" section. I have only modified settings from default as according to this Allegro tutorial: <a href="http://wiki.allegro.cc/index.php?title=Windows,_Visual_Studio_2010_and_Allegro_5" rel="nofollow">http://wiki.allegro.cc/index.php?title=Windows,_Visual_Studio_2010_and_Allegro_5</a>. It should not have affected anything in a destructive way.</p>
  
  <p>I have several files, so I will list each one. I will skip some code to reduce size and redundancy. When I omit any code or do anything not verbatim, I will put a comment in the code saying so. (EDIT: I actually did not skip any code)</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include "common.h"
  
  int main(int argc, char **argv)
  {
     ALLEGRO_DISPLAY *display = NULL;
     World* world = new World(640, 480);
  
     if(!al_init()) {
        fprintf(stderr, "failed to initialize allegro!\n");
        return -1;
     }
  
     display = al_create_display(640, 480);
     if(!display) {
        fprintf(stderr, "failed to create display!\n");
        return -1;
     }
  
     al_clear_to_color(al_map_rgb(0,0,0));
  
     world-&gt;draw(display);
  
     al_flip_display();
  
     al_rest(10.0);
  
     al_destroy_display(display);
  
     return 0;
  }
  </code></pre>
  
  <p>common.h</p>
  
  <pre><code>#if !defined(COMMON_INC)
  #define COMMON_INC
  
  #include "World.h"
  #include "Pane.h"
  #include &lt;stdio.h&gt;
  #include &lt;allegro5/allegro.h&gt;
  
  #endif
  </code></pre>
  
  <p>World.h</p>
  
  <pre><code>#if !defined(WORLD_INC)
  #define WORLD_INC
  
  #include "common.h"
  #include "Pane.h"
  
  class World{
  public:
      World(int wp, int hp);
      void draw(ALLEGRO_DISPLAY* display);
      void update();
  
  
  protected:
  
  private:
      Pane* panel;
      int heightPix, widthPix;
  
  };
  
  #endif
  </code></pre>
  
  <p>World.cpp</p>
  
  <pre><code>#include "common.h"
  
  World::World(int wp, int hp){
      widthPix = wp;
      heightPix = hp;
      panel = new Pane(this, 10, 10, 300, 400);
      return;
  }
  
  void World::draw(ALLEGRO_DISPLAY* display){
      panel-&gt;draw(display);
      return;
  }
  </code></pre>
  
  <p>Pane.h</p>
  
  <pre><code>#if !defined(PANE_INC)
  #define PANE_INC
  
  #include "common.h"
  
  class Pane{
  public:
      Pane(World* w, int xv, int yv, int wi, int he);
      World* getWorld();
      int getZ();
      void draw(ALLEGRO_DISPLAY* display);
      ALLEGRO_BITMAP* getBackground();
  
  protected:
      void setXY(int xv, int yv);
      void setWidthHeight(int wi, int he);
      void setZ(int zv);
      void setBackground(ALLEGRO_BITMAP* ba);
  
  private:
      int z;
      int x, y; //pixels
      int width, height; //pixels
      World* world;
      ALLEGRO_BITMAP* background;
  };
  
  #endif
  </code></pre>
  
  <p>Pane.cpp</p>
  
  <pre><code>#include "common.h"
  
  Pane::Pane(World* w, int xv, int yv, int wi, int he){
      this-&gt;setWidthHeight(wi, he);
      this-&gt;setXY(xv, yv);
      this-&gt;world = w;
  }
  
  World* Pane::getWorld(){
      return world;
  }
  
  void Pane::setXY(int xv, int yv){
      x = xv;
      y = yv;
      return;
  }
  
  void Pane::setWidthHeight(int wi, int he){\
      width = wi;
      height = he;
      return;
  }
  
  void Pane::setZ(int zv){
      z = zv;
      return;
  }
  
  void Pane::draw(ALLEGRO_DISPLAY* display){
      if(background != NULL)
          al_draw_bitmap(background, x, y, 0);
      else{
          background = al_create_bitmap(width, height);
  
          al_set_target_bitmap(background);
  
          al_clear_to_color(al_map_rgb(255, 0, 255));
  
          al_set_target_bitmap(al_get_backbuffer(display));
  
          al_draw_bitmap(background, x, y, 0);
      }
      return;
  }
  </code></pre>
  
  <p>The compiler produces this error report upon building: (I called the game madscientist because it is supposed to be alchemy-themed)</p>
  
  <pre><code>1&gt;------ Build started: Project: MadScientist, Configuration: Debug Win32 ------
  1&gt;  main.cpp
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(8): error C2061: syntax error : identifier 'World'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(11): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): warning C4183: 'getBackground': missing return type; assumed to be a member function returning 'int'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(18): error C2061: syntax error : identifier 'ALLEGRO_BITMAP'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(10): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\main.cpp(22): error C2660: 'World::draw' : function does not take 1 arguments
  1&gt;  World.cpp
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(8): error C2061: syntax error : identifier 'World'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(11): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): warning C4183: 'getBackground': missing return type; assumed to be a member function returning 'int'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(18): error C2061: syntax error : identifier 'ALLEGRO_BITMAP'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(10): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(6): error C2661: 'Pane::Pane' : no overloaded function takes 5 arguments
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(10): error C2511: 'void World::draw(ALLEGRO_DISPLAY *)' : overloaded member function not found in 'World'
  1&gt;          c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(7) : see declaration of 'World'
  1&gt;  Pane.cpp
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(8): error C2061: syntax error : identifier 'World'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(11): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): warning C4183: 'getBackground': missing return type; assumed to be a member function returning 'int'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(18): error C2061: syntax error : identifier 'ALLEGRO_BITMAP'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(10): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(3): error C2511: 'Pane::Pane(World *,int,int,int,int)' : overloaded member function not found in 'Pane'
  1&gt;          c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(6) : see declaration of 'Pane'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(9): error C2556: 'World *Pane::getWorld(void)' : overloaded function differs only by return type from 'int *Pane::getWorld(void)'
  1&gt;          c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9) : see declaration of 'Pane::getWorld'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(9): error C2371: 'Pane::getWorld' : redefinition; different basic types
  1&gt;          c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9) : see declaration of 'Pane::getWorld'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(10): error C2065: 'world' : undeclared identifier
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(30): error C2511: 'void Pane::draw(ALLEGRO_DISPLAY *)' : overloaded member function not found in 'Pane'
  1&gt;          c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(6) : see declaration of 'Pane'
  1&gt;  Generating Code...
  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  </code></pre>
  
  <p>You have all of the info that I do. I will summarize it for you to save the trouble of looking through everything.</p>
  
  <ul>
  <li>There are classes separated into header interfaces and source implementations</li>
  <li>common.h is included by all files. It itself includes all of the class definitions in the other header files</li>
  <li>When compiled, the compiler does not recognize classes defined in other header files as data types. Errors, as you would expect, cascade down.</li>
  <li><strong>The real-time error checker</strong> does not register <strong>any</strong> errors. When hovering over the word "World" when it is used as a datatype in <code>Pane</code>, for example, it recognizes the type perfectly. The real-time error checker is the feature that red underlines errors before compiler time.</li>
  <li>This Visual Studio 2012 Express is new except for the modifications mentioned earlier. The project was created as an empty C++ project. Before many headers and sources were add, the <code>main</code> function compiled correctly.</li>
  </ul>
  
  <p>Thank you for any responses. If you have any questions, please ask. The problems I have with Visual Studio always irk me.</p>
  
  <p><strong>EDITS:</strong></p>
  
  <p>My circular header logic is guarded by the header guards. I don't think this redundancy is a problem.</p>
  
  <p>I tried removing all includes from my header files except for includes to Allegro libraries. This seemed to work better, but there are still weird problems. Why the act of including is causing these data types to error is still a mystery. Here is the new error log:</p>
  
  <pre><code>1&gt;------ Build started: Project: MadScientist, Configuration: Debug Win32 ------
  1&gt;  main.cpp
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;  World.cpp
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(6): error C2065: 'panel' : undeclared identifier
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(11): error C2065: 'panel' : undeclared identifier
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(11): error C2227: left of '-&gt;draw' must point to class/struct/union/generic type
  1&gt;          type is ''unknown-type''
  1&gt;  Pane.cpp
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;  Generating Code...
  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  </code></pre>
  
  <p><strong>EDITS ROUND 2:</strong></p>
  
  <p>I switched my code around so the common.h only includes stdio and Allegro. All the header and source files include common.h and then any class's header file that they are using <em>individually</em>. Pane.cpp include Pane.h and common.h. World.h includes Pane.h and common.h.</p>
  
  <p>Error log reads:</p>
  
  <pre><code>1&gt;------ Build started: Project: MadScientist, Configuration: Debug Win32 ------
  1&gt;  main.cpp
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2061: syntax error : identifier 'World'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;  World.cpp
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2061: syntax error : identifier 'World'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(7): error C2661: 'Pane::Pane' : no overloaded function takes 5 arguments
  1&gt;  Pane.cpp
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
  1&gt;c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;  Generating Code...
  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  </code></pre>
  
  <p>This is how I see my include order now:</p>
  
  <ol>
  <li>main includes common</li>
  <li>common defines COMMON_INC</li>
  <li>common includes stdio and allegro</li>
  <li>main includes World.h</li>
  <li>World defines WORLD_INC</li>
  <li>World includes common, which is blocked. But this should be okay because common is already included</li>
  <li>World includes Pane</li>
  <li>Pane defines PANE_INC</li>
  <li>Pane includes common, which is blocked. But this should be okay because common is already included.</li>
  <li>Pane includes World. This is blocked because main already included World. Shouldn't this be okay since World was already included. Why do I even have compiler guards if I need to include files many times over, once for each file that uses it?</li>
  </ol>
  
  <p><strong>EDITS ROUND 3:</strong>
  I have learned a lot from the answers and comments here. It turns out that, according to Wikipedia, "circular dependencies are often introduced by inexperienced programmers who need to implement some kind of callback functionality." The mere fact that Pane uses World and World uses Pane is a design flaw. In Java, this was all fine for me. It was very easy; I thought that this was the best way to notify the entity that "possesses" an object of the object's actions.</p>
  
  <p>It turns out that this is a bad design scheme. Objects should not have dependencies to their "possessors". Instead, I need to implement a observer system, where the Pane can tell the World that it has updated its state.</p>
  
  <p>Reading wikipedia cleared up any questions that I had, so I now consider this question finished. Thanks to the contributors for putting up for a learning programmer.</p>