title: Linker error with virtual functions c++
body: <p>I am not sure what is wrong with this code, I am learning <a href="https://www.tutorialspoint.com/design_pattern/builder_pattern.htm" rel="nofollow noreferrer">Builder pattern</a>. The example code is in Java and I am trying to code the same in C++, butI am getting linker errors. I searched and read about it all and still couldn't find the right way and hence, posting it here. If I am missing something really trivial, my apologies. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;list&gt;
  #include &lt;memory&gt;
  #include &lt;conio.h&gt;
  
  using namespace std;
  using std::string;
  using std::unique_ptr;
  using std::list;
  
  class Packing
  {
  public:
      virtual string pack() = 0;
  };
  
  template&lt;typename T&gt;
  class Item
  {
  public:
      virtual string name();
      virtual Packing* packing();
      virtual float price();
  };
  
  /* 
    As per comments, I have now defined the functions in my Item class, but
    the actual definition is in the derived classes and because of these 
    definitions, I am not getting the actual output. I have provided the 
    required and actual output of this code. 
  
    I also read about CRTP and have incorporated those changes as well. But 
    still am not able to figure out how to get Items in the list. 
  */
  template&lt;typename T&gt;
  string Item&lt;T&gt;::name()
  {
      return "Item Class";
  }
  
  template&lt;typename T&gt;
  Packing* Item&lt;T&gt;::packing()
  {
      return (nullptr);
  }
  
  template&lt;typename T&gt;
  float Item&lt;T&gt;::price()
  {
      return 0.0f;
  }
  
  class Wrapper : public Packing
  {
  public:
      string pack() override
      {
          return "Wrapper";
      }
  };
  
  class Bottle : public Packing
  {
  public:
      string pack() override
      {
          return "Bottle";
      }
  };
  
  class Burger : public Item&lt;Burger&gt;
  {
  public:
      Packing* packing() override;
  };
  
  Packing* Burger::packing()
  {
      return (new Wrapper());
  }
  
  class ColdDrink : public Item&lt;ColdDrink&gt;
  {
  public:
      Packing* packing() override;
  };
  
  Packing* ColdDrink::packing()
  {
      return (new Bottle());
  }
  
  class VegBurger : public Burger
  {
  public:
      float price() override
      {
          return 25.0f;
      }
  
      string name() override
      {
          return "Veg Burger";
      }
  };
  
  class ChickenBurger : public Burger
  {
  public:
      float price() override
      {
          return 50.5f;
      }
  
      string name() override
      {
          return "Chicken Burger";
      }
  };
  
  class Coke : public Burger
  {
  public:
      float price() override
      {
          return 30.0f;
      }
  
      string name() override
      {
          return "Coke";
      }
  };
  
  class Pepsi : public Burger
  {
  public:
      float price() override
      {
          return 35.0f;
      }
  
      string name() override
      {
          return "Pepsi";
      }
  };
  
  class Meal
  {
  public:
      Meal() {}
  
      void addItem(Item&amp; item)  // This is the error place after changing my 
                                // code to use templates. The error is:       
                                // 1&gt;c:\users\xxx\documents\visual studio 
                    //2015\projects\mealbuilder\mealbuilder\mealbuilder.h(14):
                                // error C2955: 'Item': use of class template 
                                // requires template argument list
  
      {
          items.push_back(std::move(item));
      }
  
      float getCost()
      {
          float cost = 0.0f;
          for (auto&amp; item : items)
          {
              cost += item.price();
          }
  
          return cost;
      }
  
      void showItems()
      {
          for (auto&amp; item : items)
          {
              cout &lt;&lt; "Item : " &lt;&lt; item.name() &lt;&lt; endl;
              cout &lt;&lt; "Packing : " &lt;&lt; item.packing() &lt;&lt; endl;
              cout &lt;&lt; "Price : " &lt;&lt; item.price() &lt;&lt; endl &lt;&lt; endl;
          }
      }
  
  private:
      list&lt;Item&gt; items;
  };
  
  class MealBuilder
  {
  public:
      Meal prepareVegMeal()
      {
          Meal meal;
          VegBurger vegBurger;
          Coke coke;
          meal.addItem(vegBurger);
          meal.addItem(coke);
          return meal;
      }
  
      Meal prepareNonVegMeal()
      {
          Meal meal;
          ChickenBurger chickenBurger;
          Pepsi pepsi;
          meal.addItem(chickenBurger);
          meal.addItem(pepsi);
          return meal;
      }
  };
  
  int main()
  {
      MealBuilder mealBuilder;
  
      Meal vegMeal = mealBuilder.prepareVegMeal();
      cout &lt;&lt; "Veg Meal: " &lt;&lt; endl;
      vegMeal.showItems();
      cout &lt;&lt; "Total cost: " &lt;&lt; vegMeal.getCost();
  
      Meal nonVegMeal = mealBuilder.prepareNonVegMeal();
      cout &lt;&lt; "Non-Veg Meal: " &lt;&lt; endl;
      nonVegMeal.showItems();
      cout &lt;&lt; "Total cost: " &lt;&lt; nonVegMeal.getCost();
  
      _getch();
      return 0;
  }
  </code></pre>
  
  <p>Following the comments, here is the error I used to get prior to adding the definition of the Item class:</p>
  
  <pre><code>1&gt;------ Build started: Project: MealBuilder, Configuration: Debug Win32 ------
  1&gt;  MealBuilder.cpp
  1&gt;MealBuilder.obj : error LNK2001: unresolved external symbol "public: 
  virtual class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class 
  std::allocator&lt;char&gt; &gt; __thiscall Item::name(void)" (?name@Item@@UAE?AV?
  $basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
  1&gt;MealBuilder.obj : error LNK2001: unresolved external symbol "public: 
  virtual class Packing * __thiscall Item::packing(void)" (?
  packing@Item@@UAEPAVPacking@@XZ)
  1&gt;MealBuilder.obj : error LNK2001: unresolved external symbol "public: 
  virtual float __thiscall Item::price(void)" (?price@Item@@UAEMXZ)
  1&gt;C:\Users\XXX\documents\visual studio 
  2015\Projects\MealBuilder\Debug\MealBuilder.exe : fatal error LNK1120: 3 
  unresolved externals
  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  </code></pre>
  
  <p>After adding the definitions, I get the following output:</p>
  
  <pre><code>Veg Meal:
  Item : Item Class
  Packing : 00000000
  Price : 0
  
  Item : Item Class
  Packing : 00000000
  Price : 0
  
  Total cost: 0
  
  Non-Veg Meal:
  Item : Item Class
  Packing : 00000000
  Price : 0
  
  Item : Item Class
  Packing : 00000000
  Price : 0
  
  Total cost: 0
  </code></pre>
  
  <p>But the required output is:</p>
  
  <pre><code>Veg Meal
  Item : Veg Burger, Packing : Wrapper, Price : 25.0
  Item : Coke, Packing : Bottle, Price : 30.0
  Total Cost: 55.0
  
  
  Non-Veg Meal
  Item : Chicken Burger, Packing : Wrapper, Price : 50.5
  Item : Pepsi, Packing : Bottle, Price : 35.0
  Total Cost: 85.5
  </code></pre>
  
  <p>I am not sure how to change my code to get the required output. Please help.</p>
  
  <p>Thanks a lot, in advance.</p>