0

i somehow have alot of Errors, when i try to implement another given class in the Book (https://github.com/jaege/Cpp-Primer-5th-Exercises/blob/master/ch1/Sales_item.h this one). Now if i try to compile my code i only get alot of Error messages. This is my main class:

#include <iostream>
#include "Sales_item.h"



int main() {
    Sales_item item1, item2;
    std::cin >> item1 >> item2;

    if (item1.isbn() == item2.isbn()) {
        std::cout << item1 + item2 << std::endl; 
        return 0; 
    }
    else {
        std::cerr << "Data must refer to the same ISBN" << std::endl;
        return -1; 
    }

    

}

But now i dont really know how to use the other class from the Github. My header File look like this:

#include <iostream>
#include <string>

class Sales_item {
// these declarations are explained section 7.2.1, p. 270
// and in chapter 14, pages 557, 558, 561
friend std::istream& operator>>(std::istream&, Sales_item&);
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
friend bool operator<(const Sales_item&, const Sales_item&);
friend bool
operator==(const Sales_item&, const Sales_item&);
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) && defined(DEFAULT_FCNS)
    Sales_item() = default;
#else
    Sales_item(): units_sold(0), revenue(0.0) { }
#endif
    Sales_item(const std::string &book):
              bookNo(book), units_sold(0), revenue(0.0) { }
    Sales_item(std::istream &is) { is >> *this; }
public:
    // operations on Sales_item objects
    // member binary operator: left-hand operand bound to implicit this pointer
    Sales_item& operator+=(const Sales_item&);

    // 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 &lhs, const Sales_item &rhs)
{ return lhs.isbn() == rhs.isbn(); }

// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);

inline bool
operator==(const Sales_item &lhs, const Sales_item &rhs)
{
    // must be made a friend of Sales_item
    return lhs.units_sold == rhs.units_sold &&
           lhs.revenue == rhs.revenue &&
           lhs.isbn() == rhs.isbn();
}

inline bool
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
    return !(lhs == rhs); // != defined in terms of operator==
}

// assumes that both objects refer to the same ISBN
Sales_item& Sales_item::operator+=(const Sales_item& 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& lhs, const Sales_item& 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&
operator>>(std::istream& in, Sales_item& s)
{
    double price;
    in >> s.bookNo >> s.units_sold >> 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&
operator<<(std::ostream& out, const Sales_item& s)
{
    out << s.isbn() << " " << s.units_sold << " "
        << s.revenue << " " << s.avg_price();
    return out;
}

double Sales_item::avg_price() const
{
    if (units_sold)
        return revenue/units_sold;
    else
        return 0;
}

And the Sales_item itself is basically the same.

#include <iostream>
#include <string>

class Sales_item {
    // these declarations are explained section 7.2.1, p. 270
    // and in chapter 14, pages 557, 558, 561
    friend std::istream& operator>>(std::istream&, Sales_item&);
    friend std::ostream& operator<<(std::ostream&, const Sales_item&);
    friend bool operator<(const Sales_item&, const Sales_item&);
    friend bool
        operator==(const Sales_item&, const Sales_item&);
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) && defined(DEFAULT_FCNS)
    Sales_item() = default;
#else
    Sales_item() : units_sold(0), revenue(0.0) { }
#endif
    Sales_item(const std::string& book) :
        bookNo(book), units_sold(0), revenue(0.0) { }
    Sales_item(std::istream& is) { is >> *this; }
public:
    // operations on Sales_item objects
    // member binary operator: left-hand operand bound to implicit this pointer
    Sales_item& operator+=(const Sales_item&);

    // 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& lhs, const Sales_item& rhs)
{
    return lhs.isbn() == rhs.isbn();
}

// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);

inline bool
operator==(const Sales_item& lhs, const Sales_item& rhs)
{
    // must be made a friend of Sales_item
    return lhs.units_sold == rhs.units_sold &&
        lhs.revenue == rhs.revenue &&
        lhs.isbn() == rhs.isbn();
}

inline bool
operator!=(const Sales_item& lhs, const Sales_item& rhs)
{
    return !(lhs == rhs); // != defined in terms of operator==
}

// assumes that both objects refer to the same ISBN
Sales_item& Sales_item::operator+=(const Sales_item& 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& lhs, const Sales_item& 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&
operator>>(std::istream& in, Sales_item& s)
{
    double price;
    in >> s.bookNo >> s.units_sold >> 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&
operator<<(std::ostream& out, const Sales_item& s)
{
    out << s.isbn() << " " << s.units_sold << " "
        << s.revenue << " " << s.avg_price();
    return out;
}

double Sales_item::avg_price() const
{
    if (units_sold)
        return revenue / units_sold;
    else
        return 0;
}

This is my error output after compiling:

1>Lernencpp.cpp
1>Sales_item.obj : error LNK2005: "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Sales_item &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAVSales_item@@@Z) already defined in Lernencpp.obj
1>Sales_item.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Sales_item const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVSales_item@@@Z) already defined in Lernencpp.obj
1>Sales_item.obj : error LNK2005: "class Sales_item __cdecl operator+(class Sales_item const &,class Sales_item const &)" (??H@YA?AVSales_item@@ABV0@0@Z) already defined in Lernencpp.obj
1>Sales_item.obj : error LNK2005: "public: class Sales_item & __thiscall Sales_item::operator+=(class Sales_item const &)" (??YSales_item@@QAEAAV0@ABV0@@Z) already defined in Lernencpp.obj
1>Sales_item.obj : error LNK2005: "public: double __thiscall Sales_item::avg_price(void)const " (?avg_price@Sales_item@@QBENXZ) already defined in Lernencpp.obj
1>C:\Users\User\source\repos\Lernencpp\Debug\Lernencpp.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Done building project "Lernencpp.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

I would really appreciate any help, this book is alot of fun but i am kinda hardstuck right now.

  • It seems that your header file misses [header guards](https://stackoverflow.com/q/2979384/580083). They are present in the linked file: https://github.com/jaege/Cpp-Primer-5th-Exercises/blob/master/ch1/Sales_item.h#L37. – Daniel Langr Feb 03 '22 at 10:44
  • i added those, i still have the LNK2005 errors :/ – MysticKnight Feb 03 '22 at 11:18
  • Some entities are defined several times, see the error messages. For example, member function `Sales_item::avg_price` is defined in the file referred by you as Me header file" and in the file referred by you as "Sales_item". Normally, you should not place member function definitions in header files. – maksim_volodin Feb 03 '22 at 12:05
  • @maksim_volodin sorry i am pretty new, i dont really understand what you mean. should i just delete theclass part in the header? – MysticKnight Feb 03 '22 at 13:29
  • @MysticKnight You have `Lernencpp.cpp` file where `Sales_item::avg_price` is defined again. The function is defined if there is a body which goes in brackets. – maksim_volodin Feb 03 '22 at 13:47
  • I downloaded the header file from the link you shared and used your main function, my program works, are you using `Sales_item.h` and `Version_test.h` correctly? – Yujian Yao - MSFT Feb 04 '22 at 05:55
  • @YujianYao-MSFT yea it was my bad. I thought i have to create a new class and the header file, i did not know only the header file works. Its mybad i am really new to C++. – MysticKnight Feb 08 '22 at 15:43

1 Answers1

0

I observed that Sales_item.h is included in your main function, and in the link you shared, Sales_item.h includes Version_test.h, so you need to download both header files to ensure that your main function runs correctly.

Yujian Yao - MSFT
  • 945
  • 1
  • 3
  • 9