How can I initialize an array whose type is a struct?

Eljay Love-Jensen eljay@adobe.com
Wed Sep 1 17:50:00 GMT 2004


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;
}



More information about the Gcc-help mailing list