title: Undefined reference to constructor and destructor c++
body: <p>The error that I'm getting is this:</p>
  
  <pre><code>||=== Build: Debug in Building Sim (compiler: GNU GCC Compiler) ===|
  obj/Debug/main.o||In function `__static_initialization_and_destruction_0':|
  /home/josh/Documents/cpp/building_sim/main.cpp|15|undefined reference to `User::~User()'|
  /home/josh/Documents/cpp/building_sim/main.cpp|20|undefined reference to `User::~User()'|
  /home/josh/Documents/cpp/building_sim/main.cpp|20|undefined reference to `Building::~Building()'|
  /home/josh/Documents/cpp/building_sim/main.cpp|20|undefined reference to `User::~User()'|
  ||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
  </code></pre>
  
  <p>I've been having this error for a few days now and have seen a good 14-18 threads that have answers to this issue that do absolutely nothing for mine.  At this point my project is made up of five files:</p>
  
  <pre><code>main.cpp
  user.hpp
  user.cpp
  building.hpp
  building.cpp
  </code></pre>
  
  <p>main.cpp links building.hpp and user.hpp, building and user.cpp link their respective header files and building.cpp links user.hpp.</p>
  
  <p>I've tried building this project a multitude of times two different ways.  I created a shell script to build all three cpp files to .o files and then join them (in all possible orders), and I tried simply <code>g++ main.cpp building.cpp user.cpp</code> in all possible orders and I created a Code::Blocks project to link all the files together manually.  All of my header files have  include guards, but I don't think that's an issue.</p>
  
  <p>The full text of my files follows.</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "building.hpp"
  #include "user.hpp"
  
  // TODO Text parsing
  // TODO Resources
  // TODO Generations
  
  using namespace std;
  
  //----------------------------------
  //            Variables
  //----------------------------------
  //-----User------
  User user(1000);
  
  //---Buildings---
  int numBuildings = 0;
  
  Building building1(user, 10, 14, "George");
  Building buildings[] = {building1};
  
  int main(){
  
      return 0;
  }
  </code></pre>
  
  <p>user.hpp</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #ifndef user_h
  #define user_h
  
  class User{
  public:
      User();
      ~User();
  
      User(int _money);
  
      int getMoney();
      void setMoney(int _money);
  
      int getBuildings();
      void setBuildings(int _buildings);
  
  private:
      int money;
      int buildings;
  };
  
  #endif
  </code></pre>
  
  <p>user.cpp</p>
  
  <pre><code>#include "user.hpp"
  
  User::User(){
      User::money = 1000;
      User::buildings = 0;
  }
  
  User::User(int _money){
      User::money = _money;
      setBuildings(getBuildings()+1);
  }
  
  int User::getMoney(){
      return User::money;
  }
  
  void User::setMoney(int _money){
      User::money = _money;
  }
  
  int User::getBuildings(){
      return User::buildings;
  }
  
  void User::setBuildings(int _buildings){
      User::buildings = _buildings;
  }
  </code></pre>
  
  <p>building.hpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "user.hpp"
  
  #ifndef building_h
  #define building_h
  
  class Building{
  public:
      Building();
      ~Building();
  
      Building(User user);
      Building(User user, int _cost, int _payout);
      Building(User user, int _cost, int _payout, std::string _name);
  
      int getCost();
      int getPayout();
  
  private:
      int cost;
      int payout;
      std::string name;
  };
  
  #endif
  </code></pre>
  
  <p>building.cpp</p>
  
  <pre><code>#include "building.hpp"
  
  Building::Building(){
      std::cout &lt;&lt; "\n[ERROR] Default constructor used, no building created.\n";
  }
  
  Building::Building(User user){
      Building::cost = 10;
      Building::payout = 20;
      Building::name = "Generic";
      user.setBuildings(user.getBuildings()+1);
  }
  
  Building::Building(User user, int _cost, int _payout){
      Building::cost = _cost;
      Building::payout = _payout;
      Building::name = "Generic";
      user.setBuildings(user.getBuildings()+1);
  }
  
  Building::Building(User user, int _cost, int _payout, std::string _name){
      Building::cost = _cost;
      Building::payout = _payout;
      Building::name = _name;
      user.setBuildings(user.getBuildings()+1);
  }
  
  int Building::getCost(){
      return Building::cost;
  }
  
  int Building::getPayout(){
      return Building::payout;
  }
  </code></pre>