457,722 Members | 1,250 Online Need help? Post your question and get tips & solutions from a community of 457,722 IT Pros & Developers. It's quick & easy.

# Stroustrup 5.9 exercise 13 (creating a structure)

 P: n/a problem: define a /struct Date/ to keep track of dates. provide functions that read Dates from input, write Dates to output & initialize a date with date. solution: i thought of a /vector/ of /Date structures/. "Date" struct carries 3 things: month, date & year. i wrote the following code & of course it gives an error: #include #include #include struct Date { std::string month; int date; int year; }; std::vectormonth; std::cin >date; std::cin >year; create_date(month, date, year); } void print_date(Date* vdate) { std::cout << vdate->month << '\n' << vdate->date << '\n' << vdate->year << '\n'; } void print_all_dates(std::vector::iterator iter=vdate.begin(); iter != vdate.end(); ++iter) { print_date(*iter); } } int main() { date_input(vd); } --------- error ----------------------------------- 05_ex-13.cpp: In function 'void print_all_dates(std::vector
9 Replies

 P: n/a arnuld wrote: problem: define a /struct Date/ to keep track of dates. provide functions that read Dates from input, write Dates to output & initialize a date with date. solution: i thought of a /vector/ of /Date structures/. "Date" struct carries 3 things: month, date & year. i wrote the following code & of course it gives an error: #include #include #include struct Date { std::string month; int date; int year; }; std::vectormonth; std::cin >date; std::cin >year; create_date(month, date, year); } void print_date(Date* vdate) { std::cout << vdate->month << '\n' << vdate->date << '\n' << vdate->year << '\n'; } void print_all_dates(std::vector& vdates to avoid copying. for(std::vector::iterator iter=vdate.begin(); iter != vdate.end(); ++iter) { print_date(*iter); You should have print_date(&*iter); to compile. iter is an iterator, first use operator * to obtain the object it points to, then use operator & obtain a pointer to that object, which print_date() requires. } } int main() { date_input(vd); } --------- error ----------------------------------- 05_ex-13.cpp: In function 'void print_all_dates(std::vector

 P: n/a arnuld wrote: problem: define a /struct Date/ to keep track of dates. provide functions that read Dates from input, write Dates to output & initialize a date with date. solution: i thought of a /vector/ of /Date structures/. "Date" struct carries 3 things: month, date & year. i wrote the following code & of course it gives an error: #include #include #include struct Date { std::string month; int date; int year; }; std::vectormonth; std::cin >date; std::cin >year; create_date(month, date, year); } void print_date(Date* vdate) { std::cout << vdate->month << '\n' << vdate->date << '\n' << vdate->year << '\n'; } void print_all_dates(std::vector::iterator iter=vdate.begin(); iter != vdate.end(); ++iter) { print_date(*iter); <------------------------------------ } } int main() { date_input(vd); } --------- error ----------------------------------- 05_ex-13.cpp: In function 'void print_all_dates(std::vector

 P: n/a arnuld wrote: problem: define a /struct Date/ to keep track of dates. provide functions that read Dates from input, write Dates to output & initialize a date with date. solution: i thought of a /vector/ of /Date structures/. "Date" struct carries 3 things: month, date & year. i wrote the following code & of course it gives an error: #include #include #include struct Date { std::string month; int date; int year; }; std::vectormonth; std::cin >date; std::cin >year; create_date(month, date, year); } void print_date(Date* vdate) { std::cout << vdate->month << '\n' << vdate->date << '\n' << vdate->year << '\n'; } void print_all_dates(std::vector::iterator iter=vdate.begin(); iter != vdate.end(); ++iter) { print_date(*iter); } } int main() { date_input(vd); } --------- error ----------------------------------- 05_ex-13.cpp: In function 'void print_all_dates(std::vector

 P: n/a arnuld wrote: problem: define a /struct Date/ to keep track of dates. provide functions that read Dates from input, write Dates to output & initialize a date with date. solution: i thought of a /vector/ of /Date structures/. "Date" struct carries 3 things: month, date & year. i wrote the following code & of course it gives an error: #include #include #include struct Date { std::string month; int date; int year; }; std::vectormonth << '\n' << vdate->date << '\n' << vdate->year << '\n'; } Bad use of pointers for more than one reason. The user is never going to pass in a NULL one (if they do it's UB). In addition your print function isn't going to modify the date so use a const reference. There is another big reason why you'll want your function to take a const reference as you'll see below. Anyway void print_date( const Date & vdate ) { std::cout << vdate.month << '\n' << vdate.date << '\n' << vdate.year << '\n'; } Of course we can do better than specify cout as the output source. The best is simply to overload operator << for dates so std::ostream & operator<<( std::ostream & os, const Date & vdate ) { os << vdate.month << '\n' << vdate.date << '\n' << vdate.year << '\n'; return os; } void print_date( const Date & vdate ) { std::cout << vdate; } void print_all_dates(std::vector::iterator iter=vdate.begin(); iter != vdate.end(); ++iter) { print_date(*iter); } } Once again you need to pass by const reference. Although vectors are copyable it is best not to copy it but to pass it by const reference. In addition, having now rewritten the print_date function to take a const reference we can make use of the std::for_each algorithm. Thus: void print_all_dates( const std::vector< Date & vdates ) { std::for_each( vdates.begin(), vdates.end(), print_date ); } If you want to use operator<< then it's a bit more complex but something you should get used to: void print_all_dates( const std::vector< Date & vdates ) { std::copy ( vdates.begin(), vdates.end(), std::ostream_iterator< Date >( cout, "" ) // "" is a delimiter ); } Of course you won't really need to write these functions at all because their implementations are just one-liners so you use the one-liners when you need to print them all anyway. You will need to include headers Nov 9 '06 #5

 P: n/a benben wrote: what is vdate for? oops! i have removed it. You probably want const std::vector& vdates to avoid copying. yes, exactly, changed that too. You should have print_date(&*iter); to compile. iter is an iterator, first use operator * to obtain the object it points to, then use operator & obtain a pointer to that object, which print_date() requires. i changed it and it works but why /&/ works and /*/ gives an error. i mean, you asked me to use "reference" to obtain a "pointer" and it works but if i use "pointer" to obtain a "pointer" (i.e. **iter) i get an error. why? Learn to read error messages. It'll but a lot of help! i think this will take experience which takes a lot of time & i am investing my time into C++ :-) Nov 9 '06 #6 