我需要重写
我想我已经包含了所有必要的部分。提前致谢。
struct Reading {
int hour;
double temperature;
Reading(int h, double t): hour(h), temperature(t) { }
bool operator<(const Reading &r) const;
};
========
ostream& operator<<(ostream& ost, const Reading &r)
{
// unsure what to enter here
return ost;
}
========
vector<Reading> get_temps()
{
// stub version
cout << "Please enter name of input file name: ";
string name;
cin >> name;
ifstream ist(name.c_str());
if(!ist) error("can't open input file ", name);
vector<Reading> temps;
int hour;
double temperature;
while (ist >> hour >> temperature){
if (hour <0 || 23 <hour) error("hour out of range");
temps.push_back( Reading(hour,temperature));
}
}
例如这样:
bool operator <(Reading const& left, Reading const& right)
{
return left.temperature < right.temperature;
}
它应该是一个全局函数(或者与Reading
),不是会员或Reading
,它应该被声明为friend
如果您有任何受保护或私人成员。这可以像这样完成:
struct Reading {
int hour;
double temperature;
Reading(int h, double t): hour(h), temperature(t) { }
friend bool operator <(Reading const& left, Reading const& right);
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)