double mean(vector<Reading> temps)
{
// stub version
double mean_temp;
double sum = 0;
for (int i = 0; i< temps.size(); ++i) sum += temps[i];
mean_temp = sum/temps.size();
return (mean_temp);
}
double median(vector<Reading> temps)
{
// stub version
double median_temp;
sort (temps.begin(), temps.end());
median_temp = temps[temps.size()/2];
return (median_temp);
}
============================================
导致错误:
proj4.cc: In function ‘double mean(Vector<Reading>)’:
proj4.cc:132: error: no match for ‘operator+=’ in ‘sum += temps.Vector<T>::operator[] [with T = Reading](((unsigned int)i))’
proj4.cc: In function ‘double median(Vector<Reading>)’:
proj4.cc:142: error: cannot convert ‘Reading’ to ‘double’ in assignment
=============================================
以下完整代码。在我继续之前,我需要解决这两个错误
#include <bjarne/std_lib_facilities.h>
struct Reading {
int hour;
double temperature;
Reading(int h, double t): hour(h), temperature(t) { }
bool operator<(const Reading &r) const;
};
bool Reading::operator<(const Reading &r) const
{
// stub version
vector<Reading> temps;
sort (temps.begin(), temps.end());
}
/*
* function declarations
*/
ostream& operator<<(ostream& ost, const Reading &r);
vector<Reading> get_temps();
double check_adjust_temp(double temperature, char scale);
double c_to_f(double temperature);
double mean(vector<Reading> temps);
double median(vector<Reading> temps);
void print_results(const vector<Reading>& temps, double mean_temp,
double median_temp);
int main()
try
{
vector<Reading> temps = get_temps();
if (temps.size() == 0) error("no temperatures given!");
double mean_temp = mean(temps);
sort(temps.begin(), temps.end());
double median_temp = median(temps);
print_results(temps, mean_temp, median_temp);
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!\n";
return 2;
}
/*
* function definitions
*/
ostream& operator<<(ostream& ost, const Reading &r)
{
// stub version
/*
*/
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));
}
}
double check_adjust_temp(double temperature, char scale)
{
// stub version
if (scale== 'c' || 'C'){
return c_to_f(temperature);
}
else if (scale== 'f' || 'F') {
return temperature;
}
else {
error("Wrong input type");
}
}
double c_to_f(double temperature)
{
// stub version
double c;
c = ((temperature * (9.0/5)) + 32);
return (c);
}
double mean(vector<Reading> temps)
{
// stub version
double mean_temp;
double sum = 0;
for (int i = 0; i< temps.size(); ++i) sum += temps[i];
mean_temp = sum/temps.size();
return (mean_temp);
}
double median(vector<Reading> temps)
{
// stub version
double median_temp;
sort (temps.begin(), temps.end());
median_temp = temps[temps.size()/2];
return (median_temp);
}
void print_results(const vector<Reading>& temps, double mean_temp,
double median_temp)
{
// stub version
cout << "The sorted temperatures are:\n";
cout << get_temps;
cout << "The mean temperature is " << mean_temp << ".\n";
cout << "The median temperature is " << median_temp << ".\n";
}
答案 0 :(得分:4)
为阅读定义转换运算符:
struct Reading {
int hour;
double temperature;
Reading(int h, double t): hour(h), temperature(t) { }
bool operator<(const Reading &r) const;
operator double() { return temperature; }
};
或者,更好(通过最小惊讶原则),只需改变你班级的用法:
// was:
sum += temps[i];
//change to:
sum += temps[i].temperature;
// was:
median_temp = temps[temps.size()/2];
//change to:
median_temp = temps[temps.size()/2].temperature;
答案 1 :(得分:1)
您正尝试在class Reading
和double
的实例之间执行添加。只要您不提供从Reading
到double
的默认转化路径,此功能就无效:
Reading::operator double() const { return temperature; }
或通过提供适当的全局运算符+()重载:
double operator+(Reading const&, double);
double operator+(double, Reading const&);
第二个错误应该可以通过Reading::operator double()
解决,如上所示。
答案 2 :(得分:0)
您正在尝试将阅读对象概括为双变量!
sum += temps[i];
sum类型是double,而temps [i]返回Reading对象,你不能求和double
和Reading
对象,因为它们属于不同类型,你必须定义加号的重载运算符并使编译器了解如何完成。
答案 3 :(得分:0)
对于阅读课程
,您缺少算子+ =