c++ - 类的非成员接口引发的编译错误?
天蓬老师
天蓬老师 2017-04-17 13:59:56
0
1
289

//在C++ Primer中文版230页Sales_data类中,有个问题不太明白。当接口函数只有 combine的时候,编译成功,一旦加入 read 和 print两个非成员接口函数,则编译报错,请指教

ifndef SALES_DATA_H_INCLUDED

define SALES_DATA_H_INCLUDED

include

include

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;

}

endif // SALES_DATA_H_INCLUDED

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all (1)
左手右手慢动作

请在#include 后面加上一句using namespace std;

贴代码的时候不要直接贴,使用添加代码的功能。

#ifndef SALES_DATA_H_INCLUDED #define SALES_DATA_H_INCLUDED #include  #include  using namespace std; 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); } #endif /* SALES_DATA_H_INCLUDED */
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!