友元函数编译出错。
//定义一个将两个sales_item对象相加的非成员函数。#include <iostream>
#include<string>
using namespace std;
class sales_item
{
//将Add函数声明为sales_item的友元。
friend sales_item Add(const sales_item& obj1,const sales_item& obj2);
public:
sales_item(const string &book="fuck!"):isbn(book),units_sold(0),revenue(0.0)
{
}
sales_item(istream &is=cin)
{
cin >> isbn >> units_sold >> revenue;
}
void print()
{
std::cout<<isbn<<" "<<units_sold<<" "<<revenue<<endl;
}
private:
int units_sold;
double revenue;
string isbn;
};
//Add函数执行相加操作。
sales_item Add(const sales_item &obj1,const sales_item &obj2);
sales_item Add(const sales_item &obj1,const sales_item &obj2)
{
sales_item temp;
temp.isbn=obj1.isbn;
temp.units_sold=obj1.units_sold+obj2.units_sold;
temp.revenue=obj1.revenue+obj2.revenue;
return temp;
}
int main()
{
sales_item s1(cin);
sales_item s2(cin);
s1.print();
s2.print();
cout<<endl;
sales_item item3;
item3=Add(const& s1,const& s2);
return 0;
}