title: Using other Class C++ Primer 5th Edition
body: <p>i somehow have alot of Errors, when i try to implement another given class in the Book (<a href="https://github.com/jaege/Cpp-Primer-5th-Exercises/blob/master/ch1/Sales_item.h" rel="nofollow noreferrer">https://github.com/jaege/Cpp-Primer-5th-Exercises/blob/master/ch1/Sales_item.h</a> this one). Now if i try to compile my code i only get alot of Error messages.
  This is my main class:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &quot;Sales_item.h&quot;
  
  
  
  int main() {
      Sales_item item1, item2;
      std::cin &gt;&gt; item1 &gt;&gt; item2;
  
      if (item1.isbn() == item2.isbn()) {
          std::cout &lt;&lt; item1 + item2 &lt;&lt; std::endl; 
          return 0; 
      }
      else {
          std::cerr &lt;&lt; &quot;Data must refer to the same ISBN&quot; &lt;&lt; std::endl;
          return -1; 
      }
  
      
  
  }
  </code></pre>
  <p>But now i dont really know how to use the other class from the Github.
  My header File look like this:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  class Sales_item {
  // these declarations are explained section 7.2.1, p. 270
  // and in chapter 14, pages 557, 558, 561
  friend std::istream&amp; operator&gt;&gt;(std::istream&amp;, Sales_item&amp;);
  friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const Sales_item&amp;);
  friend bool operator&lt;(const Sales_item&amp;, const Sales_item&amp;);
  friend bool
  operator==(const Sales_item&amp;, const Sales_item&amp;);
  public:
      // constructors are explained in section 7.1.4, pages 262 - 265
      // default constructor needed to initialize members of built-in type
  #if defined(IN_CLASS_INITS) &amp;&amp; defined(DEFAULT_FCNS)
      Sales_item() = default;
  #else
      Sales_item(): units_sold(0), revenue(0.0) { }
  #endif
      Sales_item(const std::string &amp;book):
                bookNo(book), units_sold(0), revenue(0.0) { }
      Sales_item(std::istream &amp;is) { is &gt;&gt; *this; }
  public:
      // operations on Sales_item objects
      // member binary operator: left-hand operand bound to implicit this pointer
      Sales_item&amp; operator+=(const Sales_item&amp;);
  
      // operations on Sales_item objects
      std::string isbn() const { return bookNo; }
      double avg_price() const;
  // private members as before
  private:
      std::string bookNo;      // implicitly initialized to the empty string
  #ifdef IN_CLASS_INITS
      unsigned units_sold = 0; // explicitly initialized
      double revenue = 0.0;
  #else
      unsigned units_sold;
      double revenue;
  #endif
  };
  
  // used in chapter 10
  inline
  bool compareIsbn(const Sales_item &amp;lhs, const Sales_item &amp;rhs)
  { return lhs.isbn() == rhs.isbn(); }
  
  // nonmember binary operator: must declare a parameter for each operand
  Sales_item operator+(const Sales_item&amp;, const Sales_item&amp;);
  
  inline bool
  operator==(const Sales_item &amp;lhs, const Sales_item &amp;rhs)
  {
      // must be made a friend of Sales_item
      return lhs.units_sold == rhs.units_sold &amp;&amp;
             lhs.revenue == rhs.revenue &amp;&amp;
             lhs.isbn() == rhs.isbn();
  }
  
  inline bool
  operator!=(const Sales_item &amp;lhs, const Sales_item &amp;rhs)
  {
      return !(lhs == rhs); // != defined in terms of operator==
  }
  
  // assumes that both objects refer to the same ISBN
  Sales_item&amp; Sales_item::operator+=(const Sales_item&amp; rhs)
  {
      units_sold += rhs.units_sold;
      revenue += rhs.revenue;
      return *this;
  }
  
  // assumes that both objects refer to the same ISBN
  Sales_item
  operator+(const Sales_item&amp; lhs, const Sales_item&amp; rhs)
  {
      Sales_item ret(lhs);  // copy (|lhs|) into a local object that we'll return
      ret += rhs;           // add in the contents of (|rhs|)
      return ret;           // return (|ret|) by value
  }
  
  std::istream&amp;
  operator&gt;&gt;(std::istream&amp; in, Sales_item&amp; s)
  {
      double price;
      in &gt;&gt; s.bookNo &gt;&gt; s.units_sold &gt;&gt; price;
      // check that the inputs succeeded
      if (in)
          s.revenue = s.units_sold * price;
      else
          s = Sales_item();  // input failed: reset object to default state
      return in;
  }
  
  std::ostream&amp;
  operator&lt;&lt;(std::ostream&amp; out, const Sales_item&amp; s)
  {
      out &lt;&lt; s.isbn() &lt;&lt; &quot; &quot; &lt;&lt; s.units_sold &lt;&lt; &quot; &quot;
          &lt;&lt; s.revenue &lt;&lt; &quot; &quot; &lt;&lt; s.avg_price();
      return out;
  }
  
  double Sales_item::avg_price() const
  {
      if (units_sold)
          return revenue/units_sold;
      else
          return 0;
  }
  </code></pre>
  <p>And the Sales_item itself is basically the same.</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  class Sales_item {
      // these declarations are explained section 7.2.1, p. 270
      // and in chapter 14, pages 557, 558, 561
      friend std::istream&amp; operator&gt;&gt;(std::istream&amp;, Sales_item&amp;);
      friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const Sales_item&amp;);
      friend bool operator&lt;(const Sales_item&amp;, const Sales_item&amp;);
      friend bool
          operator==(const Sales_item&amp;, const Sales_item&amp;);
  public:
      // constructors are explained in section 7.1.4, pages 262 - 265
      // default constructor needed to initialize members of built-in type
  #if defined(IN_CLASS_INITS) &amp;&amp; defined(DEFAULT_FCNS)
      Sales_item() = default;
  #else
      Sales_item() : units_sold(0), revenue(0.0) { }
  #endif
      Sales_item(const std::string&amp; book) :
          bookNo(book), units_sold(0), revenue(0.0) { }
      Sales_item(std::istream&amp; is) { is &gt;&gt; *this; }
  public:
      // operations on Sales_item objects
      // member binary operator: left-hand operand bound to implicit this pointer
      Sales_item&amp; operator+=(const Sales_item&amp;);
  
      // operations on Sales_item objects
      std::string isbn() const { return bookNo; }
      double avg_price() const;
      // private members as before
  private:
      std::string bookNo;      // implicitly initialized to the empty string
  #ifdef IN_CLASS_INITS
      unsigned units_sold = 0; // explicitly initialized
      double revenue = 0.0;
  #else
      unsigned units_sold;
      double revenue;
  #endif
  };
  
  // used in chapter 10
  inline
  bool compareIsbn(const Sales_item&amp; lhs, const Sales_item&amp; rhs)
  {
      return lhs.isbn() == rhs.isbn();
  }
  
  // nonmember binary operator: must declare a parameter for each operand
  Sales_item operator+(const Sales_item&amp;, const Sales_item&amp;);
  
  inline bool
  operator==(const Sales_item&amp; lhs, const Sales_item&amp; rhs)
  {
      // must be made a friend of Sales_item
      return lhs.units_sold == rhs.units_sold &amp;&amp;
          lhs.revenue == rhs.revenue &amp;&amp;
          lhs.isbn() == rhs.isbn();
  }
  
  inline bool
  operator!=(const Sales_item&amp; lhs, const Sales_item&amp; rhs)
  {
      return !(lhs == rhs); // != defined in terms of operator==
  }
  
  // assumes that both objects refer to the same ISBN
  Sales_item&amp; Sales_item::operator+=(const Sales_item&amp; rhs)
  {
      units_sold += rhs.units_sold;
      revenue += rhs.revenue;
      return *this;
  }
  
  // assumes that both objects refer to the same ISBN
  Sales_item
  operator+(const Sales_item&amp; lhs, const Sales_item&amp; rhs)
  {
      Sales_item ret(lhs);  // copy (|lhs|) into a local object that we'll return
      ret += rhs;           // add in the contents of (|rhs|)
      return ret;           // return (|ret|) by value
  }
  
  std::istream&amp;
  operator&gt;&gt;(std::istream&amp; in, Sales_item&amp; s)
  {
      double price;
      in &gt;&gt; s.bookNo &gt;&gt; s.units_sold &gt;&gt; price;
      // check that the inputs succeeded
      if (in)
          s.revenue = s.units_sold * price;
      else
          s = Sales_item();  // input failed: reset object to default state
      return in;
  }
  
  std::ostream&amp;
  operator&lt;&lt;(std::ostream&amp; out, const Sales_item&amp; s)
  {
      out &lt;&lt; s.isbn() &lt;&lt; &quot; &quot; &lt;&lt; s.units_sold &lt;&lt; &quot; &quot;
          &lt;&lt; s.revenue &lt;&lt; &quot; &quot; &lt;&lt; s.avg_price();
      return out;
  }
  
  double Sales_item::avg_price() const
  {
      if (units_sold)
          return revenue / units_sold;
      else
          return 0;
  }
  </code></pre>
  <p>This is my error output after compiling:</p>
  <pre><code>1&gt;Lernencpp.cpp
  1&gt;Sales_item.obj : error LNK2005: &quot;class std::basic_istream&lt;char,struct std::char_traits&lt;char&gt; &gt; &amp; __cdecl operator&gt;&gt;(class std::basic_istream&lt;char,struct std::char_traits&lt;char&gt; &gt; &amp;,class Sales_item &amp;)&quot; (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAVSales_item@@@Z) already defined in Lernencpp.obj
  1&gt;Sales_item.obj : error LNK2005: &quot;class std::basic_ostream&lt;char,struct std::char_traits&lt;char&gt; &gt; &amp; __cdecl operator&lt;&lt;(class std::basic_ostream&lt;char,struct std::char_traits&lt;char&gt; &gt; &amp;,class Sales_item const &amp;)&quot; (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVSales_item@@@Z) already defined in Lernencpp.obj
  1&gt;Sales_item.obj : error LNK2005: &quot;class Sales_item __cdecl operator+(class Sales_item const &amp;,class Sales_item const &amp;)&quot; (??H@YA?AVSales_item@@ABV0@0@Z) already defined in Lernencpp.obj
  1&gt;Sales_item.obj : error LNK2005: &quot;public: class Sales_item &amp; __thiscall Sales_item::operator+=(class Sales_item const &amp;)&quot; (??YSales_item@@QAEAAV0@ABV0@@Z) already defined in Lernencpp.obj
  1&gt;Sales_item.obj : error LNK2005: &quot;public: double __thiscall Sales_item::avg_price(void)const &quot; (?avg_price@Sales_item@@QBENXZ) already defined in Lernencpp.obj
  1&gt;C:\Users\User\source\repos\Lernencpp\Debug\Lernencpp.exe : fatal error LNK1169: one or more multiply defined symbols found
  1&gt;Done building project &quot;Lernencpp.vcxproj&quot; -- FAILED.
  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
  </code></pre>
  <p>I would really appreciate any help, this book is alot of fun but i am kinda hardstuck right now.</p>