2
\$\begingroup\$

I have these struct(s) to keep data about temperature measurement:

// data structure representing day 
struct Day{
   vector<double>hour;
   Day();
}; 
Day::Day(): hour(24){
   for(int i=0; i< hour.size();++i) hour[i]=not_a_reading;
}

// data structure representing month
struct Month{
    int month;
    vector<Day>day;
    Month(): month(not_a_month),day(32) { }
};

// data structure representing year
struct Year{
    int year;
    vector<Month>month;
    Year(): month(12) { }
};

// vector holding measured data
vector<Year>yrs;

I am trying to write a program to print all the data inside yrs. Here is what I got so far:

void print_year(ostream os, vector<Year>& v){
for(size_t i=0; i<v.size(); ++i){
    os << "{year " << v[i].year; 
    for (size_t j=0; j<v[i].month.size(); ++j){
        os << " {month " << v[i].month[j].month;
        for (size_t k=0; k<v[i].month[j].day.size();++k){
            os << '(' <<v[i].month[j].day <<" "<< v[i].month[j].day[k].hour;
            //--^^---- (No << operator matches these operands) WHY?
            os  << ') ';
            }
        os << "}";
        }
    os << "}";
    }
}

If I have one measurement on: 10th of Jan 1992 at 10 o'clock with temperature 61.6 Fahrenheit and two on: 11th and 22th of Feb 1992 with temperatures: 64 and 65.2. The output (format) will be: (year 1992 { month jan ( 10 61.5)} {month feb (11 64) (22 65.2)})

I'm probably using the vector access operator[] wrong.

I was trying to fix it and got to this:

void print_year(ostream os, vector<Year>& v){
for(size_t i=0; i<v.size(); ++i){
    os << "{year " << v[i].year; 
    for (size_t j=0; j<v[i].month.size(); ++j){
        os << " {month " << month_input_tb[v[i].month[j].month];
        for (size_t k=0; k< v[i].month[j].day.size();++k){
            os <<  '('<< k <<" "; // loop counter coincides with day of the month
            for (size_t z=0; z < v[i].month[j].day[k].hour.size(); ++z){
                os << v[i].month[j].day[k].hour[z];
                os  << ')';
        }
        os << "}";
    }
    os << "}";
}

}

Then I saw your (Loki Astari) answer and I am writing separate << operators now.

\$\endgroup\$
3
\$\begingroup\$

I would try and simplify that each structure knows how to print itself:

int main()
{
    Year    year;
    // Load data into year.

    std::cout << year;

    // You can print a vector of years with:
    std::vector<Year>   yrs;

    std::copy(std::begin(yrs), std::end(yrs),
              std::ostream_iterator<Year>(std::cout, "\n"));

    // or a Simple loop:
    for(auto const& year: yrs) {
        std::cout << year << "\n";
    }

};

To make this work you need to define an operator<< for Year

struct Year{
    int year;
    vector<Month>month;
    Year(): month(12) { }

    friend std::ostream& operator<<(std::ostream& str, Year const& year)
    {
        str << "{year " << year.year;
        for(auto const& month : year.month) {
            str << month;
        }
        str << "}"; 
        return str;
    }
};

So the print for Year is defined in terms of printing for a Month object. So you know have to define how to print a Month. Which in turn will print some Day objects. So you will also have to define how to print a Day object.

The problem you are seeing:

os << '(' << v[i].month[j].day <<" "<< v[i].month[j].day[k].hour;
//           ^^^^^^^^^^^^^^^^^
//    This is a vector.
//    The stream class does not know how to stream a vector.
//    Did you mean `v[i].month[j].month`
\$\endgroup\$
  • \$\begingroup\$ so, to reduce the print function to multiple << operators for each object separately. \$\endgroup\$ – Ziezi Apr 14 '15 at 19:12
  • \$\begingroup\$ @simplicisveritatis: Yes. \$\endgroup\$ – Martin York Apr 14 '15 at 19:24
  • \$\begingroup\$ I updated the question to answer the question at the end. :) \$\endgroup\$ – Ziezi Apr 14 '15 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.