//在C++ Primer中文版230页Sales_data类中,有个问题不太明白。当接口函数只有 combine的时候,编译成功,一旦加入 read 和 print两个非成员接口函数,则编译报错,请指教
struct Sales_data{
std::string isbn()const {return bookNo;}
Sales_data& combine(const Sales_data &rhs);
std::string bookNo;
unsigned uints_sold =0;
double revenue = 0.0;
};
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
uints_sold += rhs.uints_sold;
revenue += rhs.revenue;
return *this;
}
std::istream &read (istream &is , Sales_data &item)
{
double price = 0;
is >> item.bookNo >> item.uints_sold >> price;
item.revenue = price * item.uints_sold ;
return is;
}
std::ostream &print(ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.uints_sold << " " << item.revenue << " ";
return os;
}
Please add
#include <string>
using namespace std;
afterWhen posting the code, do not paste it directly, use the function of adding code.