* How can I initialize an array whose type is a struct?
@ 2004-09-01 16:37 learning c++
2004-09-01 17:50 ` Eljay Love-Jensen
0 siblings, 1 reply; 2+ messages in thread
From: learning c++ @ 2004-09-01 16:37 UTC (permalink / raw)
To: gcc-help
I have a short code to print the month and days in a month. I declared a
struct including month and days. but it does not work.
Please help me.
#include<iostream>
#include<vector>
using namespace std;
struct year{
char* month;
int day;
};
int main(){
vector<year>arr(("Jan",31),("Feb",28));
vector<year>::iterator iter;
for(iter=arr.begin(); iter!=arr.end(); iter++)
cout<<iter->month<<" "<<iter->days<<endl;
return 0;
}
_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*
http://join.msn.com/?page=features/junkmail
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: How can I initialize an array whose type is a struct?
2004-09-01 16:37 How can I initialize an array whose type is a struct? learning c++
@ 2004-09-01 17:50 ` Eljay Love-Jensen
0 siblings, 0 replies; 2+ messages in thread
From: Eljay Love-Jensen @ 2004-09-01 17:50 UTC (permalink / raw)
To: learning c++, gcc-help
Hi,
>How can I initialize an array whose type is a struct?
You aren't initializing an array, you are initializing a std::vector.
You cannot construct a std::vector that way, as you did in your code. You
can construct arrays like that (with the appropriate syntax).
For a std::vector, use the push_back method.
Also, "iter->days" should be "iter->day".
HTH,
--Eljay
- - - - - - - - - - - - - - - - - - -
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using std::vector;
struct year
{
year(string const& m, int d)
: month(m), day(d)
{ }
string month;
int day;
};
int main()
{
vector<year> arr;
arr.push_back(year("Jan", 31));
arr.push_back(year("Feb", 28));
vector<year>::iterator iter;
for(iter = arr.begin(); iter != arr.end(); ++iter)
cout << iter->month << " " << iter->day << endl;
}
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2004-09-01 17:50 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-01 16:37 How can I initialize an array whose type is a struct? learning c++
2004-09-01 17:50 ` Eljay Love-Jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).