标题:错在哪里?谢谢
只看楼主
DG中专生
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2007-9-11
 问题点数:0 回复次数:4 
错在哪里?谢谢
#ifndef B
#define B
#include<iostream>
#include<string>
class b{
public:
 std::istream& input(std::istream& in);
 std::ostream& output(std::ostream& out) const;
 double a() const;
 bool isbn(const b &rhs) const
 {
  return isbn==rhs.isbn;
 }
private:
 std::string isbn;
 unsigned units_sold;
 double revenue;
};
#endif
std::istream& b::input(std::istream& in)
{
 double price;
 in>>isbn>>units_sold>>price;
 if(in)
  revenue=units_sold*price;
 else{
  units_sold=0;
  revenue=0.0;
 }
 return in;
}
std::ostream& b::output(std::ostream& out) const
{
 out<<isbn<<"\t"<units_sold<<"\t"<<revenue<<"\t"<<a();
 return out;
}
double b::a() const
{
 if(units_sold)
  return revenue/units_sold;
 else
  return 0;
}
#include<iostream>
using namespace std;
int main()
{
 b item;
 cout<<"Enter"<<endl;
 while(item.input(cin))
 {
  cout<<item.output(cout);
  cout<<endl;
 }
 return 0;
}

请问错在哪里,还有,高手可以解释一下类定义中代码的意思吗,我不太明白,谢谢
2007-12-05 22:19
DG中专生
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2007-9-11
得分:0 
刚机子卡了下,连续发了2个贴
2007-12-05 22:56
duccdd
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-25
得分:0 
bool [bold]isbn[/bold](const b &rhs) const
{
  return isbn==rhs.isbn;
}
private:
std::string[bold] isbn[/bold];

[bold]重定义isbn[/bold]

out<<isbn<<"\t" [bold]< [/bold]units_sold<<"\t"<<revenue<<"\t"<<a(); [bold]少了一个<
[/bold]
2007-12-05 23:40
rainyee
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2007-10-20
得分:0 
rep
把头文件与主文件分开,刚才测试了下,你的out<<isbn<<"\t"<units_sold<<"\t"<<revenue<<"\t"<<a();
中少了个<在units_sold前面,不是输出流了!

當你在 穿山越嶺的另一邊 我在孤獨的路上 沒有盡頭......
2007-12-05 23:55
rainyee
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2007-10-20
得分:0 
rep
我刚才忘了说isbn函数与私有数据重名了,重新定义一下吧

當你在 穿山越嶺的另一邊 我在孤獨的路上 沒有盡頭......
2007-12-05 23:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-190639-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.127973 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved