我有练习1.20学习C++。使用入门书
它说下载这个头文件Sales_item.h
/***********************************************************
* filename: sales_item.h
**********************************************************/
#ifndef __SALES_ITEM_H__
#define __SALES_ITEM_H__
#include <iostream>
#include <string>
#include <stdexcept>
//using std::istream; using std::ostream;
class Sales_item
{
/* operations on sales_item object */
public:
double avg_price(void) const;
bool same_isbn(const Sales_item &rhs) const /* inline function */
{
return isbn == rhs.isbn; /* return this->isbn == rhs.isbn*/
}
std::istream &read_item(std::istream &in);
std::ostream &write_item(std::ostream &out) const;
// bool add_item(const sales_item &rhs);
Sales_item add_item(const Sales_item &other);
/* default constructor --- constructor without parameters */
/* default constructor needed to initialize members of built-in type */
Sales_item(): units_sold(0), revenue(0.0) {}
/* private members as before */
private:
std::string isbn; /* ISBN */
unsigned units_sold; /* the number of book sold */
double revenue; /* the total revenue from that book */
};
#if 0
inline
double sales_item::avg_price(void) const
{
if (units_sold)
return (revenue/units_sold); /* return (this->revenue/this->units_sold); */
else
return 0;
}
#endif
inline
double Sales_item::avg_price(void) const
{
using std::runtime_error;
try
{
if (units_sold == 0)
throw runtime_error("when units_sold equal 0, error...");
return (revenue/units_sold); /* return (this->revenue/this->units_sold); */
}
catch (runtime_error err)
{
std::cout << err.what() << std::endl;
return 0;
}
}
#endif /* __SALES_ITEM_H__ */
,然后用这个代码中使用了一个问题:
#include <iostream>
#include "Sales_item.h"
int main(int argc, const char * argv[])
{
Sales_item book;
int gne;
// read ISBN, number of copies sold, and sales price
std::cin >> book;
// write ISBN, number of copies sold, total revenue, and average price
std::cout << book << std::endl;
return 0;
}
我在std :: cin和std :: cout行收到错误。两个错误是:
无效的操作数的二进制表达式( '的IStream'(又名 'basic_istream')和 '的Sales_item')
我使用的Xcode 5.0。 它可能是什么?
“C +”,是真的吗? –