0

重複の可能性:
C ++:<<演算子coutintおよびdouble値に書き換えます

<<演算子を書き直して、時間(int)と温度(double)の値を計算できるようにする必要があります。

必要なセクションはすべて含めたと思います。問題のセクションはostream&operator <<

それは私にエラーを与えます:

proj4.cc:87: error: ‘r->Reading::hour’ cannot be used as a function
proj4.cc:88: error: ‘r->Reading::temperature’ cannot be used as a function

それらをr.hourとr.temperatureとして単純に書き直すことはできますか?

ありがとう。

=================

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)
{
    return ost << '(' << r.hour()
               << ',' << r.temperature() <<')';

}

========

vector<Reading> get_temps()
{                                                             
    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));
    }

}

4

0 に答える 0